首页 > 代码库 > SEH and C++ Exceptions,自定义CSeException
SEH and C++ Exceptions,自定义CSeException
Description of CSeException
CSeException
class is based on CException
class provided by MFC. I overwrite some of useful methods, but it is working same way like any other exception class based on CException
class - you can find description in documentation provided by Visual C++.
By Martin Ziacek
Source Files :
[cpp] view plain copy print?
- //////////////////////////////////////////////////////////////////////////
- // SeException.h By Martin Ziacek
- #ifndef __SEEXCEPTION_H__
- #define __SEEXCEPTION_H__
- class CSeException : public CException
- {
- DECLARE_DYNAMIC(CSeException)
- public:
- CSeException(UINT nSeCode, _EXCEPTION_POINTERS* pExcPointers);
- CSeException(CSeException & CseExc);
- _EXCEPTION_POINTERS* GetSePointers(void);
- PVOID GetExceptionAddress(void);
- UINT GetSeCode(void);
- void Delete(void);
- int ReportError(UINT nType = MB_OK, UINT nIDHelp = 0);
- BOOL GetErrorMessage(CString & CsErrDescr, PUINT pnHelpContext = NULL);
- BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL);
- private:
- UINT m_nSeCode;
- _EXCEPTION_POINTERS* m_pExcPointers;
- };
- void SeTranslator(UINT nSeCode, _EXCEPTION_POINTERS* pExcPointers);
- #endif //__SEEXCEPTION_H__
[cpp] view plain copy print?
- //////////////////////////////////////////////////////////////////////////
- // SeException.cpp By Martin Ziacek
- #include "stdafx.h"
- #include "SeException.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define CASE(nSeCode,CsString) case EXCEPTION_##nSeCode: /
- CsString.Format(_T("Exception %s (0x%.8x) at address 0x%.8x."),_T(#nSeCode),EXCEPTION_##nSeCode,m_pExcPointers->ExceptionRecord->ExceptionAddress); /
- break;
- void SeTranslator(UINT nSeCode, _EXCEPTION_POINTERS* pExcPointers)
- {
- throw new CSeException(nSeCode,pExcPointers);
- }
- IMPLEMENT_DYNAMIC(CSeException,CException)
- CSeException::CSeException(UINT nSeCode, _EXCEPTION_POINTERS* pExcPointers)
- {
- m_nSeCode = nSeCode;
- m_pExcPointers = pExcPointers;
- }
- CSeException::CSeException(CSeException & CseExc)
- {
- m_nSeCode = CseExc.m_nSeCode;
- m_pExcPointers = CseExc.m_pExcPointers;
- }
- UINT CSeException::GetSeCode()
- {
- return m_nSeCode;
- }
- _EXCEPTION_POINTERS* CSeException::GetSePointers()
- {
- return m_pExcPointers;
- }
- PVOID CSeException::GetExceptionAddress()
- {
- return m_pExcPointers->ExceptionRecord->ExceptionAddress;
- }
- void CSeException::Delete(void)
- {
- #ifdef _DEBUG
- m_bReadyForDelete = TRUE;
- #endif
- delete this;
- }
- int CSeException::ReportError(UINT nType/* = MB_OK*/, UINT nIDHelp/* = 0*/)
- {
- int rc;
- CString strMessage;
- GetErrorMessage(strMessage);
- rc = AfxMessageBox(strMessage,nType,nIDHelp);
- return rc;
- }
- BOOL CSeException::GetErrorMessage(CString & CsErrDescr, PUINT pnHelpContext/* = NULL*/)
- {
- BOOL rc = TRUE;
- if (pnHelpContext != NULL)
- *pnHelpContext = 0;
- switch (m_nSeCode) {
- CASE(ACCESS_VIOLATION,CsErrDescr);
- CASE(DATATYPE_MISALIGNMENT,CsErrDescr);
- CASE(BREAKPOINT,CsErrDescr);
- CASE(SINGLE_STEP,CsErrDescr);
- CASE(ARRAY_BOUNDS_EXCEEDED,CsErrDescr);
- CASE(FLT_DENORMAL_OPERAND,CsErrDescr);
- CASE(FLT_DIVIDE_BY_ZERO,CsErrDescr);
- CASE(FLT_INEXACT_RESULT,CsErrDescr);
- CASE(FLT_INVALID_OPERATION,CsErrDescr);
- CASE(FLT_OVERFLOW,CsErrDescr);
- CASE(FLT_STACK_CHECK,CsErrDescr);
- CASE(FLT_UNDERFLOW,CsErrDescr);
- CASE(INT_DIVIDE_BY_ZERO,CsErrDescr);
- CASE(INT_OVERFLOW,CsErrDescr);
- CASE(PRIV_INSTRUCTION,CsErrDescr);
- CASE(IN_PAGE_ERROR,CsErrDescr);
- CASE(ILLEGAL_INSTRUCTION,CsErrDescr);
- CASE(NONCONTINUABLE_EXCEPTION,CsErrDescr);
- CASE(STACK_OVERFLOW,CsErrDescr);
- CASE(INVALID_DISPOSITION,CsErrDescr);
- CASE(GUARD_PAGE,CsErrDescr);
- CASE(INVALID_HANDLE,CsErrDescr);
- default:
- CsErrDescr = _T("Unknown exception.");
- rc = FALSE;
- break;
- }
- return rc;
- }
- BOOL CSeException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext/* = NULL*/)
- {
- ASSERT(lpszError != NULL && AfxIsValidString(lpszError, nMaxError));
- if (pnHelpContext != NULL)
- *pnHelpContext = 0;
- CString strMessage;
- GetErrorMessage(strMessage);
- if ((UINT)strMessage.GetLength() >= nMaxError) {
- lpszError[0] = 0;
- return FALSE;
- } else {
- lstrcpyn(lpszError, strMessage, nMaxError);
- return TRUE;
- }
- }
Demo Project:
[cpp] view plain copy print?
- void CSehDemoDlg::OnBtnCreateException()
- {
- char *p = NULL;
- try
- {
- p[0] = 0;
- }
- catch(CSeException *e)
- {
- TCHAR trcMsg[1024];
- e->GetErrorMessage(trcMsg,1024);
- TRACE(trcMsg);
- TRACE(_T("/n"));
- e->ReportError(MB_OK | MB_ICONSTOP);
- e->Delete();
- }
- }
The Result:
From: http://www.codeproject.com/KB/cpp/seexception.aspx
http://blog.csdn.net/wangningyu/article/details/4579127
SEH and C++ Exceptions,自定义CSeException
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。