home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / EXCEPT.CP_ / EXCEPT.CP
Encoding:
Text File  |  1993-02-08  |  5.0 KB  |  189 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CException
  24.  
  25. IMPLEMENT_DYNAMIC(CException, CObject)      // abstract class
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // AFX_EXCEPTION_CONTEXT (thread global state)
  29.  
  30. #ifndef _AFXDLL
  31. // single threaded, assume 1 global exception context
  32. AFX_EXCEPTION_CONTEXT NEAR afxExceptionContext;
  33. #endif
  34.  
  35. void AFX_EXCEPTION_CONTEXT::Throw(CException* pNewException)
  36. {
  37.     // default to not shared
  38.     Throw(pNewException, FALSE);
  39. }
  40.  
  41. void AFX_EXCEPTION_CONTEXT::ThrowLast()
  42. {
  43.     // default to not shared, use the old one
  44.     ASSERT(m_pCurrent != NULL);
  45.  
  46.     Throw(m_pCurrent, FALSE);
  47. }
  48.  
  49. #ifndef _WINDOWS
  50. extern "C" void __cdecl longjmp(int[_AFX_JBLEN], int);
  51. #endif
  52.  
  53.  
  54. void AFX_EXCEPTION_CONTEXT::Throw(CException* pNewException, BOOL bShared)
  55. {
  56.     ASSERT(pNewException != NULL);
  57.     ASSERT_VALID(pNewException);
  58.  
  59.     TRACE1("Warning: Throwing an Exception of type %Fs\n",
  60.         pNewException->GetRuntimeClass()->m_lpszClassName);
  61.  
  62.     if (m_pCurrent != pNewException)
  63.     {
  64.         // throwing a new exception (otherwise keep old shared state)
  65.         if (m_pCurrent != NULL && m_bDeleteWhenDone)
  66.             delete m_pCurrent;
  67.         m_pCurrent = pNewException;
  68.         m_bDeleteWhenDone = !bShared;
  69.     }
  70.  
  71.     while (1)
  72.     {
  73.         // walk the handlers
  74.         if (m_pLinkTop == NULL)
  75.         {
  76.             // uncaught exception, terminate
  77.             TRACE1("Error: Un-caught Exception (%Fs)\n",
  78.                 pNewException->GetRuntimeClass()->m_lpszClassName);
  79.             AfxTerminate();
  80.         }
  81.  
  82.         AFX_EXCEPTION_LINK* pReceiver = m_pLinkTop;
  83.         m_pLinkTop = m_pLinkTop->m_pLinkPrev;
  84.         pReceiver->m_pLinkPrev = NULL;
  85.  
  86.         if (pReceiver->m_nType == 0)
  87.         {
  88. #ifdef _WINDOWS
  89.             ::Throw(pReceiver->m_jumpBuf, 1);
  90. #else
  91.             ::longjmp(pReceiver->m_jumpBuf, 1);
  92. #endif
  93.             ASSERT(FALSE);      // not reached
  94.         }
  95.         // otherwise just call cleanup proc
  96.         (*pReceiver->m_callback.pfnCleanup)(pReceiver);
  97.     }
  98.     ASSERT(FALSE);      // not reached
  99. }
  100.  
  101. void AFX_EXCEPTION_CONTEXT::Cleanup()
  102. {
  103.     if (m_bDeleteWhenDone)
  104.         delete m_pCurrent;
  105.     m_pCurrent = NULL;
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // AFX_EXCEPTION_LINK linked 'jmpbuf' and out-of-line helpers
  110.  
  111. AFX_EXCEPTION_LINK::AFX_EXCEPTION_LINK()
  112. {
  113.     m_pLinkPrev = afxExceptionContext.m_pLinkTop;
  114.     afxExceptionContext.m_pLinkTop = this;
  115.     m_nType = 0;
  116. }
  117.  
  118. // out-of-line implementation of CATCH and AND_CATCH
  119. BOOL AFXAPI AfxCatchProc(CRuntimeClass* pClass)
  120. {
  121.     ASSERT(pClass != NULL);
  122.     ASSERT(afxExceptionContext.m_pCurrent != NULL);
  123.     return (afxExceptionContext.m_pCurrent->IsKindOf(pClass));
  124. }
  125.  
  126. // out-of-line implementation of END_CATCH (throw uncaught exception)
  127. void AFXAPI AfxEndCatchProc()
  128. {
  129.     ASSERT(afxExceptionContext.m_pCurrent != NULL);
  130.     THROW_LAST();
  131. }
  132.  
  133. AFX_EXCEPTION_LINK::~AFX_EXCEPTION_LINK()
  134. {
  135.     if (afxExceptionContext.m_pLinkTop == this)
  136.     {
  137.         // remove ourself from the top of the chain
  138.         afxExceptionContext.m_pLinkTop = m_pLinkPrev;
  139.     }
  140.     else if (m_pLinkPrev == NULL)
  141.     {
  142.         // cleanup any used exceptions at end of uncaught chain
  143.         if (m_nType == 0)
  144.             afxExceptionContext.Cleanup();
  145.     }
  146.     else
  147.     {
  148.         // something went wrong
  149.         TRACE0("Error: AFX_EXCEPTION_LINK destructor terminating app\n");
  150.         AfxTerminate(); 
  151.     }
  152. }
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // Global exception terminate handling - Obsolete API
  156. //   (useful for non-Windows MS-DOS apps only)
  157.  
  158. #ifndef _AFXDLL
  159. static AFX_TERM_PROC NEAR pfnTerminate = AfxAbort;
  160.  
  161. void AFXAPI AfxTerminate()
  162. {
  163.     TRACE0("AfxTerminate called\n");
  164.     (*pfnTerminate)();
  165. }
  166.  
  167. AFX_TERM_PROC AFXAPI AfxSetTerminate(AFX_TERM_PROC pfnNew)
  168. {
  169.     AFX_TERM_PROC pfnOld = pfnTerminate;
  170.     pfnTerminate = pfnNew;
  171.     return pfnOld;
  172. }
  173. #endif //!_AFXDLL
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // Standard exceptions
  177.  
  178. IMPLEMENT_DYNAMIC(CMemoryException, CException)
  179. static  CMemoryException NEAR simpleMemoryException; 
  180. void AFXAPI AfxThrowMemoryException()                           
  181.     { afxExceptionContext.Throw(&simpleMemoryException, TRUE); }
  182.  
  183. IMPLEMENT_DYNAMIC(CNotSupportedException, CException)
  184. static  CNotSupportedException NEAR simpleNotSupportedException; 
  185. void AFXAPI AfxThrowNotSupportedException()                         
  186.     { afxExceptionContext.Throw(&simpleNotSupportedException, TRUE); }
  187.  
  188. /////////////////////////////////////////////////////////////////////////////
  189.