SEH and C + + Exceptions, custom cseexception

Source: Internet
Author: User

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?
  1. //////////////////////////////////////////////////////////////////////////
  2. SeException.h by Martin Ziacek
  3. #ifndef __seexception_h__
  4. #define __seexception_h__
  5. Class Cseexception: public cexception
  6. {
  7. Declare_dynamic (cseexception)
  8. Public
  9. Cseexception (UINT nsecode, _exception_pointers* pexcpointers);
  10. Cseexception (cseexception & cseexc);
  11. _exception_pointers* getsepointers (void);
  12. PVOID getexceptionaddress (void);
  13. UINT Getsecode (void);
  14. void Delete (void);
  15. int ReportError (uint nType = mb_ok, uint NIDHELP = 0);
  16. BOOL geterrormessage (CString & cserrdescr, puint pnhelpcontext = NULL);
  17. BOOL geterrormessage (LPTSTR lpszerror, UINT nmaxerror, puint pnhelpcontext = NULL);
  18. Private
  19. UINT m_nsecode;
  20. _exception_pointers* m_pexcpointers;
  21. };
  22. void Setranslator (UINT nsecode, _exception_pointers* pexcpointers);
  23. #endif//__seexception_h__

[cpp]View PlainCopyprint?
  1. //////////////////////////////////////////////////////////////////////////
  2. SeException.cpp by Martin Ziacek
  3. #include "stdafx.h"
  4. #include "SeException.h"
  5. #ifdef _DEBUG
  6. #define NEW Debug_new
  7. #endif
  8. #undef This_file
  9. static char this_file[] = __file__;
  10. #define CASE (nsecode,csstring) exception_# #nSeCode:/
  11. Csstring.format (_t ("Exception%s (0x%.8x) at address 0x%.8x."), _t (#nSeCode), exception_# #nSeCode, m_pexcpointers- >exceptionrecord->exceptionaddress); /  
  12. break ;
  13. void Setranslator (UINT nsecode, _exception_pointers* pexcpointers)
  14. {
  15. throw New Cseexception (nsecode,pexcpointers);
  16. }
  17. Implement_dynamic (cseexception,cexception)
  18. Cseexception::cseexception (UINT nsecode, _exception_pointers* pexcpointers)
  19. {
  20. M_nsecode = nsecode;
  21. M_pexcpointers = pexcpointers;
  22. }
  23. Cseexception::cseexception (cseexception & Cseexc)
  24. {
  25. M_nsecode = cseexc.m_nsecode;
  26. M_pexcpointers = cseexc.m_pexcpointers;
  27. }
  28. UINT Cseexception::getsecode ()
  29. {
  30. return m_nsecode;
  31. }
  32. _exception_pointers* cseexception::getsepointers ()
  33. {
  34. return m_pexcpointers;
  35. }
  36. PVOID cseexception::getexceptionaddress ()
  37. {
  38. return m_pexcpointers->exceptionrecord->exceptionaddress;
  39. }
  40. void Cseexception::D elete (void)
  41. {
  42. #ifdef _DEBUG
  43. M_breadyfordelete = TRUE;
  44. #endif
  45. Delete this ;
  46. }
  47. int Cseexception::reporterror (uint nType/* = mb_ok*/, UINT nidhelp/* = 0*/)
  48. {
  49. int rc;
  50. CString strmessage;
  51. GetErrorMessage (strmessage);
  52. rc = AfxMessageBox (strmessage,ntype,nidhelp);
  53. return rc;
  54. }
  55. BOOL cseexception::geterrormessage (CString & cserrdescr, puint pnhelpcontext/* = null*/)
  56. {
  57. BOOL rc = TRUE;
  58. if (pnhelpcontext! = NULL)
  59. *pnhelpcontext = 0;
  60. Switch (m_nsecode) {
  61. Case (access_violation,cserrdescr);
  62. Case (datatype_misalignment,cserrdescr);
  63. Case (breakpoint,cserrdescr);
  64. Case (single_step,cserrdescr);
  65. Case (array_bounds_exceeded,cserrdescr);
  66. Case (flt_denormal_operand,cserrdescr);
  67. Case (flt_divide_by_zero,cserrdescr);
  68. Case (flt_inexact_result,cserrdescr);
  69. Case (flt_invalid_operation,cserrdescr);
  70. Case (flt_overflow,cserrdescr);
  71. Case (flt_stack_check,cserrdescr);
  72. Case (flt_underflow,cserrdescr);
  73. Case (int_divide_by_zero,cserrdescr);
  74. Case (int_overflow,cserrdescr);
  75. Case (priv_instruction,cserrdescr);
  76. Case (in_page_error,cserrdescr);
  77. Case (illegal_instruction,cserrdescr);
  78. Case (noncontinuable_exception,cserrdescr);
  79. Case (stack_overflow,cserrdescr);
  80. Case (invalid_disposition,cserrdescr);
  81. Case (guard_page,cserrdescr);
  82. Case (invalid_handle,cserrdescr);
  83. default:
  84. CSERRDESCR = _t ("Unknown exception.");
  85. rc = FALSE;
  86. break ;
  87. }
  88. return rc;
  89. }
  90. BOOL cseexception::geterrormessage (LPTSTR lpszerror, UINT nmaxerror, puint pnhelpcontext/* = Null*/)
  91. {
  92. ASSERT (lpszerror! = NULL && afxisvalidstring (lpszerror, nmaxerror));
  93. if (pnhelpcontext! = NULL)
  94. *pnhelpcontext = 0;
  95. CString strmessage;
  96. GetErrorMessage (strmessage);
  97. if (UINT) strmessage.getlength () >= nmaxerror) {
  98. lpszerror[0] = 0;
  99. return FALSE;
  100. } Else {
  101. Lstrcpyn (lpszerror, strmessage, nmaxerror);
  102. return TRUE;
  103. }
  104. }

Demo Project:

[cpp]View PlainCopyprint?
  1. void Csehdemodlg::onbtncreateexception ()
  2. {
  3. char *p = NULL;
  4. Try
  5. {
  6. p[0] = 0;
  7. }
  8. Catch (cseexception *e)
  9. {
  10. TCHAR trcmsg[1024];
  11. E->geterrormessage (trcmsg,1024);
  12. TRACE (trcmsg);
  13. TRACE (_t ("/n"));
  14. E->reporterror (MB_OK | mb_iconstop);
  15. E->delete ();
  16. }
  17. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.