home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / EXCEPT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  3.4 KB  |  136 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   owl\source\except.cpp
  4. //   Defines type TXOwl.
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\except.h>
  8. #include <owl\module.h>
  9.  
  10.  
  11. //
  12. // mbModalFlag() determines the best MB modal flag to use in the current
  13. // situation. Uses MB_TASKMODAL if under NT, or the task/thread has at least
  14. // one toplevel window. Uses MB_SYSTEMMODAL for win32s/win16 when there are
  15. // no windows.
  16. //
  17. #if defined(__WIN32__)
  18.  
  19. BOOL CALLBACK threadHasWnd(HWND, BOOL* has) { *has = TRUE; return FALSE; }
  20.  
  21. static unsigned mbModalFlag()
  22. {
  23.   if (!(HIWORD(::GetVersion()) & 0x8000))   // NT can always open task modal
  24.     return MB_TASKMODAL;
  25.   BOOL has = FALSE;
  26.   ::EnumThreadWindows(GetCurrentThreadId(), (WNDENUMPROC)threadHasWnd,
  27.                       LPARAM(&has));
  28.   return has ? MB_TASKMODAL : MB_SYSTEMMODAL;
  29. }
  30.  
  31. #else
  32.  
  33. BOOL CALLBACK taskHasWnd(HWND, BOOL far* has) { *has = TRUE; return FALSE; }
  34.  
  35. static unsigned mbModalFlag()
  36. {
  37.   BOOL has = FALSE;
  38.   ::EnumTaskWindows(GetCurrentTask(), (WNDENUMPROC)taskHasWnd,
  39.                     LPARAM((BOOL far*)&has));
  40.   return has ? MB_TASKMODAL : MB_SYSTEMMODAL;
  41. }
  42.  
  43. #endif
  44.  
  45. //
  46. // Global exception handler used when an application object is not available.
  47. // May be overriden by user code by redefining this function.  If a valid
  48. // application object is found by GetApplicationObject, then the virtual
  49. // TModule::Error(TXOwl& x, char* caption, BOOL canResume) is usually used
  50. // instead.
  51. //
  52. int _OWLFUNC
  53. HandleGlobalException(xmsg& x, char* caption, char* canResume)
  54. {
  55.   char errorStr[255];
  56.   int  buttons = MB_OK;
  57.   int  len = x.why().length();
  58.  
  59.   if (!caption)
  60.     caption = "Unhandled Exception";
  61.   if (len)
  62.     strcpy(errorStr, x.why().c_str());
  63.   else {
  64.     strcpy(errorStr, "Unknown Exception");
  65.     len = strlen(errorStr);
  66.   }
  67.   if (canResume) {
  68.     buttons = MB_YESNO;
  69.     errorStr[len] = '\n';
  70.     strcpy(errorStr+len+1, canResume);
  71.   }
  72.   return ::MessageBox(0, errorStr, caption,
  73.                       mbModalFlag() | MB_ICONSTOP | buttons) == IDYES ? 0 : -1;
  74. }
  75.  
  76. //
  77. // Static member function used to convert a resource id to a 'string'.  This
  78. // is necessary since we must pass a string to the xmsg base class
  79. // constructor.  Sets found to TRUE if the resource was located, otherwise
  80. // FALSE.  In either case, the string is initialized to something
  81. // printable.
  82. //
  83. string
  84. TXOwl::ResourceIdToString(BOOL* found, unsigned resId, TModule* module)
  85. {
  86.   char buf[128];
  87.  
  88.   BOOL status = (module && module->LoadString(resId, buf, sizeof(buf)) != 0);
  89.   if (found)
  90.     *found = status;
  91.  
  92.   if (!status)
  93.     wsprintf(buf, "Exception #%u (no message available).", resId);
  94.  
  95.   string rscStr(buf);
  96.   return rscStr;
  97. }
  98.  
  99. TXOwl::TXOwl(unsigned resId, TModule* module)
  100.   : xmsg(ResourceIdToString(0, resId, module))
  101. {
  102. }
  103.  
  104. int
  105. TXOwl::Unhandled(TModule* app, unsigned promptResId)
  106. {
  107.   return app->Error(*this, IDS_OWLEXCEPTION, promptResId);
  108. }
  109.  
  110. TXOwl*
  111. TXOwl::Clone()
  112. {
  113.   return new TXOwl(*this);
  114. }
  115.  
  116. void
  117. TXOwl::Throw()
  118. {
  119.   THROW( *this );
  120. }
  121.  
  122. TXOutOfMemory::TXOutOfMemory() : TXOwl(IDS_OUTOFMEMORY)
  123. {
  124. }
  125.  
  126. TXOwl*
  127. TXOutOfMemory::Clone()
  128. {
  129.   return new TXOutOfMemory(*this);
  130. }
  131. void
  132. TXOutOfMemory::Throw()
  133. {
  134.   THROW( *this );
  135. }
  136.