except.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. /////////////////////////////////////////////////////////////////////////////
  12. // AFX_EXCEPTION_CONTEXT (thread global state)
  13. // WINSCP
  14. AFX_EXCEPTION_CONTEXT __thread m_exceptionContext;
  15. inline AFX_EXCEPTION_CONTEXT* AfxGetExceptionContext()
  16. {
  17. DWORD lError = GetLastError();
  18. AFX_EXCEPTION_CONTEXT* pContext = &m_exceptionContext;
  19. SetLastError(lError);
  20. return pContext;
  21. }
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CException
  24. CException::CException()
  25. {
  26. }
  27. void CException::Delete()
  28. {
  29. delete this;
  30. }
  31. BOOL CException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  32. PUINT pnHelpContext /* = NULL */ )
  33. {
  34. if (pnHelpContext != NULL)
  35. *pnHelpContext = 0;
  36. if (nMaxError != 0 && lpszError != NULL)
  37. *lpszError = '\0';
  38. return FALSE;
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. // AFX_EXCEPTION_LINK linked 'jmpbuf' and out-of-line helpers
  42. AFX_EXCEPTION_LINK::AFX_EXCEPTION_LINK()
  43. {
  44. // setup initial link state
  45. m_pException = NULL; // no current exception yet
  46. // wire into top of exception link stack
  47. AFX_EXCEPTION_CONTEXT* pContext = AfxGetExceptionContext();
  48. m_pLinkPrev = pContext->m_pLinkTop;
  49. pContext->m_pLinkTop = this;
  50. }
  51. // out-of-line cleanup called from inline AFX_EXCEPTION_LINK destructor
  52. void AFXAPI AfxTryCleanup()
  53. {
  54. AFX_EXCEPTION_CONTEXT* pContext = AfxGetExceptionContext();
  55. AFX_EXCEPTION_LINK* pLinkTop = pContext->m_pLinkTop;
  56. // delete current exception
  57. ASSERT(pLinkTop != NULL);
  58. if (pLinkTop->m_pException != NULL)
  59. pLinkTop->m_pException->Delete();
  60. // remove ourself from the top of the chain
  61. pContext->m_pLinkTop = pLinkTop->m_pLinkPrev;
  62. }
  63. // special out-of-line implementation of THROW_LAST (for auto-delete behavior)
  64. void AFXAPI AfxThrowLastCleanup()
  65. {
  66. AFX_EXCEPTION_CONTEXT* pContext = AfxGetExceptionContext();
  67. AFX_EXCEPTION_LINK* pLinkTop = pContext->m_pLinkTop;
  68. // check for THROW_LAST inside of auto-delete block
  69. if (pLinkTop != NULL)
  70. {
  71. // make sure current exception does not get auto-deleted
  72. pLinkTop->m_pException = NULL;
  73. }
  74. // THROW_LAST macro will do actual 'throw'
  75. }