Description of Cseexception
CSeExceptionClass is based on CException class provided by MFC. I overwrite some of useful methods, but it's working same the "like" any and exception class based on CException class-you C An find description in documentation provided by Visual C + +.
by Martin Ziacek
Source Files:
[cpp]View PlainCopyprint?
- //////////////////////////////////////////////////////////////////////////
- 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 PlainCopyprint?
- //////////////////////////////////////////////////////////////////////////
- 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) 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::D elete (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 PlainCopyprint?
- 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, custom cseexception