Try-Catch 未捕获异常
Try-Catch doesn't catch exception
我想捕获一个异常,它发生在我的代码使用Try/catch像这样:
try
{
//my code
}
catch(std::exception &e)
{
std::wcout <<"An exception occured"<<" "<<e.what();
}
,但是这样的结构不能捕获我代码中的异常。我在这一行收到Visual studio的一个异常:
retcode = bcp_init(hdbc1,"information1", NULL,NULL, DB_IN);
and not from my try/catch.
在这里你可以看到我的全部代码:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include<tchar.h>
#include<iostream>
#include<sqlncli.h>
#include<exception>
using namespace std;
SQLHENV henv = SQL_NULL_HENV;
HDBC hdbc1 = SQL_NULL_HDBC, hdbc2 = SQL_NULL_HDBC;
SQLHSTMT hstmt2 = SQL_NULL_HSTMT;
void Cleanup() {
if (hstmt2 != SQL_NULL_HSTMT)
SQLFreeHandle(SQL_HANDLE_STMT, hstmt2);
if (hdbc1 != SQL_NULL_HDBC) {
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
}
if (hdbc2 != SQL_NULL_HDBC) {
SQLDisconnect(hdbc2);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc2);
}
if (henv != SQL_NULL_HENV)
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
int main() {
try
{
RETCODE retcode;
// BCP variables.
char *terminator = " ";
// bcp_done takes a different format return code because it returns number of rows bulk copied
// after the last bcp_batch call.
DBINT cRowsDone = 0;
// Set up separate return code for bcp_sendrow so it is not using the same retcode as SQLFetch.
RETCODE SendRet;
// Allocate the ODBC environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLAllocHandle(Env) Failednn");
Cleanup();
return(9);
}
// Notify ODBC that this is an ODBC 3.0 app.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLSetEnvAttr(ODBC version) Failednn");
Cleanup();
return(9);
}
// Allocate ODBC connection handle, set bulk copy mode, and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLAllocHandle(hdbc1) Failednn");
Cleanup();
return(9);
}
retcode = SQLSetConnectAttr(hdbc1, SQL_COPT_SS_BCP, (void *)SQL_BCP_ON, SQL_IS_INTEGER);
if ( (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
printf("SQLSetConnectAttr(hdbc1) Failednn");
Cleanup();
return(9);
}
// sample uses Integrated Security, create the SQL Server DSN using Windows NT authentication
SQLWCHAR dsn[30] = L"mssqltest"; //Name DNS
SQLWCHAR user[10] = L"di_test";
SQLWCHAR pass[10] = L"di_test";
// SQLWCHAR tb[20]=L"information1";
retcode = SQLConnectW(hdbc1, (SQLWCHAR *)dsn, SQL_NTS, (SQLWCHAR *) user, SQL_NTS, (SQLWCHAR *) pass, SQL_NTS);
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("SQLConnect() Failednn");
Cleanup();
return(9);
}
// TRYODBC(hdbc1, SQL_HANDLE_DBC, retcode);
// Initialize the bulk copy.
retcode = bcp_init(hdbc1,"information1", NULL,NULL, DB_IN);
/* TRYODBC(hdbc1, SQL_HANDLE_DBC, retcode);*/
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("bcp_init(hdbc1) Failednn");
Cleanup();
return(9);
}
//Define our array
DBINT custIDs[] ={1,2,3,4};
// Bind the program variables for the bulk copy.
if( bcp_bind(hdbc1, (BYTE *)&custIDs, 0, sizeof(DBINT), NULL,0, SQLINT4, 2)==FAIL)
{
retcode=-1;
}
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("bcp_bind(hdbc1) Failednn");
Cleanup();
return(9);
}
// Could normally use strlen to calculate the bcp_bind cbTerm parameter, but this terminator
// is a null byte ( ), which gives strlen a value of 0. Explicitly give cbTerm a value of 1.
if( bcp_bind(hdbc1,(LPCBYTE) custIDs, 2, SQL_VARLEN_DATA, NULL,0, SQL_C_NUMERIC, 3)==FAIL)
{
retcode=-1;
}
if ( (retcode != SUCCEED) ) {
printf("bcp_bind(hdbc1) Failednn");
Cleanup();
return(9);
}
if ( (SendRet = bcp_sendrow(hdbc1) ) != SUCCEED ) {
printf("bcp_sendrow(hdbc1) Failednn");
Cleanup();
return(9);
}
cRowsDone = bcp_done(hdbc1);
if ( (cRowsDone == -1) ) {
printf("bcp_done(hdbc1) Failednn");
Cleanup();
return(9);
}
printf("Number of rows bulk copied after last bcp_batch call = %d.n", cRowsDone);
// Cleanup.
SQLFreeHandle(SQL_HANDLE_STMT, hstmt2);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLDisconnect(hdbc2);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc2);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
catch(std::exception &e)
{
std::wcout <<"An exception occured"<<" "<<e.what();
}
}
您没有看到c++异常。这就是微软所说的SEH异常,它实际上是一个程序崩溃。你可以捕捉到这些(在MSDN上搜索__try和__except),但一般来说,这就像Java的NullPointerException——这是一个编程错误,没有错误处理将修复它。
同样,Visual Studio,如果设置为"break on all exceptions",当一个异常发生时,也会在try/catch中处理。如果你继续执行,它会转到相应的catch &处理。如果发生了这种情况,请关闭"所有异常断开"。
c++不是Java。没有通用基类异常这样的东西,所以std::exception
只是标准库抛出的所有异常的基类。
如果想捕获任何异常(而不是可能发生的奇怪的错误),可以使用catch(...)
。
如SingerOfTheFall的注释所示,您可以使用c++ 11 std::current_exception来检查异常对象
相关文章:
- 当类定义不可见时捕获异常
- 有没有更好的方法来处理异常? try-catch块真的很丑
- 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?
- std::unique_ptr 在 try-catch 块中未捕获取消引用异常
- 构造函数中的异常:init() 方法、指针、大型 try/catch 或
- 基类catch不捕获异常,即使它出现在派生类catch之前
- 为什么当我抛出派生类的对象时,基类的 catch 块会捕获异常
- 格式化文件异常被 try catch 块忽略
- libpng错误不会被try/catch块捕获
- "try catch(...)"可以在 C++ 中捕获哪些异常?
- MFC TRY CATCH与C++对MFC异常的尝试捕获
- 使用try-Catch异常处理程序和if-else条件检查之间的区别
- c++异常处理程序中嵌套的try..catch
- Try-Catch 未捕获异常
- 未处理的异常,甚至在添加try-catch块之后?c++
- Catch(…)没有捕获异常,我的程序仍然崩溃
- try和catch块没有捕获异常,而是将结果作为无穷大
- 捕获在没有 try/catch 的情况下引发的异常
- 将主应用程序对象包含在 try-catch 块中捕获所有崩溃
- 在try catch块中未捕获异常