home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK10 / MFC / SRC / EXCEPT.CP$ / except
Encoding:
Text File  |  1992-03-03  |  3.7 KB  |  139 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 documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #include "afx.h"
  12. #pragma hdrstop
  13.  
  14. #ifdef AFX_CORE_SEG
  15. #pragma code_seg(AFX_CORE_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24.  
  25. #if defined(_WINDOWS) && defined(_DOSWIN)
  26. extern "C" void far pascal Throw(const int FAR*, int);
  27. #define longjmp ::Throw
  28. #else
  29. extern "C" void __cdecl longjmp(jmp_buf, int);
  30. #endif
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // single threaded, assume 1 global exception context
  34.  
  35. CExceptionContext  NEAR afxExceptionContext;
  36.  
  37. IMPLEMENT_DYNAMIC(CException, CObject)      // abstract class
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // class CExceptionContext (thread global state)
  41.  
  42. void 
  43. CExceptionContext::Throw(CException* pNewException)
  44. {
  45.     // default to not shared
  46.     Throw(pNewException, FALSE);
  47. }
  48.  
  49. void 
  50. CExceptionContext::ThrowLast()
  51. {
  52.     // default to not shared, use the old one
  53.     ASSERT(m_pCurrent != NULL);
  54.  
  55.     Throw(m_pCurrent, FALSE);
  56. }
  57.  
  58.  
  59. void 
  60. CExceptionContext::Throw(CException* pNewException, BOOL bShared)
  61. {
  62.     ASSERT(pNewException != NULL);
  63.     TRACE("Warning: Throwing an Exception of type %s\n",
  64.         pNewException->GetRuntimeClass()->m_pszClassName);
  65.  
  66.     if (m_pCurrent != pNewException)
  67.     {
  68.         // throwing a new exception (otherwise keep old shared state)
  69.         if (m_pCurrent != NULL && m_bDeleteWhenDone)
  70.             delete m_pCurrent;
  71.         m_pCurrent = pNewException;
  72.         m_bDeleteWhenDone = !bShared;
  73.     }
  74.  
  75.     // walk the handlers
  76.     if (m_pLinkTop == NULL)
  77.     {
  78.         // uncaught exception, terminate
  79.         TRACE("Error: Un-caught Exception (%s)\n",
  80.             pNewException->GetRuntimeClass()->m_pszClassName);
  81.         AfxTerminate();
  82.     }
  83.  
  84.     CExceptionLink* pReceiver = m_pLinkTop;
  85.     m_pLinkTop = m_pLinkTop->m_pLinkPrev;
  86.     pReceiver->m_pLinkPrev = NULL;
  87.     longjmp(pReceiver->m_jumpBuf, 1);
  88. }
  89.  
  90. void 
  91. CExceptionContext::Cleanup()
  92. {
  93.     if (m_bDeleteWhenDone)
  94.         delete m_pCurrent;
  95.     m_pCurrent = NULL;
  96. }
  97.  
  98.  
  99. CExceptionLink::~CExceptionLink()
  100. {
  101.     if (afxExceptionContext.m_pLinkTop == this)
  102.         afxExceptionContext.m_pLinkTop = m_pLinkPrev;
  103.     else if (m_pLinkPrev != NULL)
  104.         AfxTerminate(); 
  105. }
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // Support for new exception APIs
  109.  
  110. static AFX_TERM_PROC pfnTerminate = AfxAbort;
  111.  
  112. void CDECL AfxTerminate()
  113. {
  114.     TRACE("AfxTerminate called\n");
  115.     (*pfnTerminate)();
  116. }
  117.  
  118. AFX_TERM_PROC AfxSetTerminate(AFX_TERM_PROC pfnNew)
  119. {
  120.     AFX_TERM_PROC pfnOld = pfnTerminate;
  121.     pfnTerminate = pfnNew;
  122.     return pfnOld;
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // Standard exceptions
  127.  
  128. IMPLEMENT_DYNAMIC(CMemoryException, CException)
  129. static  CMemoryException NEAR simpleMemoryException; 
  130. void PASCAL AfxThrowMemoryException()                           
  131.     { afxExceptionContext.Throw(&simpleMemoryException, TRUE); }
  132.  
  133. IMPLEMENT_DYNAMIC(CNotSupportedException, CException)
  134. static  CNotSupportedException NEAR simpleNotSupportedException; 
  135. void PASCAL AfxThrowNotSupportedException()                         
  136.     { afxExceptionContext.Throw(&simpleNotSupportedException, TRUE); }
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139.