| #ifndef _AFX_OLD_EXCEPTIONS #define TRY { AFX_EXCEPTION_LINK _afxExceptionLink; try { #define CATCH(class, e) } catch (class* e) \ { ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \ _afxExceptionLink.m_pException = e; #define AND_CATCH(class, e) } catch (class* e) \ { ASSERT(e->IsKindOf(RUNTIME_CLASS(class))); \ _afxExceptionLink.m_pException = e; #define END_CATCH } } #define THROW(e) throw e #define THROW_LAST() (AfxThrowLastCleanup(), throw) // Advanced macros for smaller code #define CATCH_ALL(e) } catch (CException* e) \ { { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \ _afxExceptionLink.m_pException = e; #define AND_CATCH_ALL(e) } catch (CException* e) \ { { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \ _afxExceptionLink.m_pException = e; #define END_CATCH_ALL } } } #define END_TRY } catch (CException* e) \ { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \ _afxExceptionLink.m_pException = e; } } |
| 异常类 | 含义 |
| CMemoryException | 内存不足 |
| CFileException | 文件异常 |
| CArchiveException | 存档/序列化异常 |
| CNotSupportedException | 响应对不支持服务的请求 |
| CResourceException | Windows 资源分配异常 |
| CDaoException | 数据库异常(DAO 类) |
| CDBException | 数据库异常(ODBC 类) |
| COleException | OLE 异常 |
| COleDispatchException | 调度(自动化)异常 |
| CUserException | 用消息框警告用户然后引发一般 CException 的异常 |
| #include <iostream.h> #include "afxwin.h" int main() { TRY { CFile f( "d:\\1.txt", CFile::modeWrite ); } CATCH( CFileException, e ) { if( e->m_cause == CFileException::fileNotFound ) cout << "ERROR: File not found\n" << endl; } END_CATCH } |
| enum { none, generic, fileNotFound, badPath, tooManyOpenFiles, accessDenied, invalidFile, removeCurrentDir, directoryFull, badSeek, hardIO, sharingViolation, lockViolation, diskFull, endOfFile }; |
| #include <iostream.h> #include "afxwin.h" int main() { TRY { AfxThrowFileException(CFileException::fileNotFound); } CATCH( CFileException, e ) { if( e->m_cause == CFileException::fileNotFound ) cout << "ERROR: File not found\n" << endl; } END_CATCH } |