📄 errorhandling.cpp
字号:
#include <eh.h> // Required for Win32 Exception Handling
#include <stdio.h> // Optional, specific to this sample
#include <afxwin.h> // Required for MFC Exception Handling
#include <afxdisp.h> // Required for MFC Exception Handling
#import <msado15.dll> rename("EOF", "adoEOF")
struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
// Class for Win32 Structured Exception Handling
class SEH_Exception {
private:
unsigned int m_uSECode;
public:
SEH_Exception(unsigned int uSECode) : m_uSECode(uSECode) {}
SEH_Exception() {}
~SEH_Exception() {}
unsigned int getSeHNumber() { return m_uSECode; }
};
// Raise Win32 Exception within a C++ Class SEH_Exception
static void MappingSEHtoCPPExceptions
(
unsigned int uExceptionCode,
_EXCEPTION_POINTERS*
)
{
throw SEH_Exception( uExceptionCode );
}
// Sets up mapping between Win32 SEH & C++ Exception Handling
void LogEnable()
{
_set_se_translator(MappingSEHtoCPPExceptions);
}
void main( void )
{
try
{
ADODB::_ConnectionPtr Conn1 = NULL;
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
LogEnable();
// To see each of the catch blocks work, comment out all but
// one of the following 4 lines, each of which throws an
// exception.
// Bogus DataSource Name, raises _com_error
Conn1->Open( L"", L"", L"", -1 );
// Explicitly raise MFC Exception
AfxThrowMemoryException();
// Explicitly raise SEH Excetption
RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 0, NULL);
// Explicitly raise exception of a type not caught below
// i.e. to the catch handlers, an "Unknown exception"
throw 0L; // Raises exception of type long
}
// Catch Blocks -- All of which are a subset of the
// Exception Handling demonstrated in LOG.CPP
// in the C++ Rosetta Stone Samples
catch( CException *e )
{
CString strName;
CRuntimeClass *pClass = e->GetRuntimeClass();
printf( "MFC Exception(%s) thrown\n",
(LPCTSTR) pClass->m_lpszClassName );
}
catch( _com_error &e )
{
printf( "#import encountered failed HRESULT\n" );
printf( "\tCode = %08lx\n", e.Error());
}
catch( SEH_Exception &e )
{
printf( "Win32 Structured Exception Raised\n" );
printf( "\t Error Code %08lx\n", e.getSeHNumber() );
}
catch(...)
{
printf( "Caught an exception of unknown type\n" );
}
::MessageBox( NULL, "Success!", "", MB_OK );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -