OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
except.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TXOwl, the base exception class for OWL exceptions
7/// that can forward handling to the app module by default.
8//----------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/window.h>
11#include <owl/except.h>
12#include <owl/module.h>
13#include <stdio.h>
14
15namespace owl {
16
18
19//
20// Global exception handler used when an application object is not available.
21// May be overriden by user code by redefining this function. Note that the
22// program must be linked statically to OWL in order for an overridden version
23// of this function to be called by the framework. If a valid
24// application object is found by GetApplicationObject, then the virtual
25// TModule::Error(TXOwl& x, char* caption, bool canResume) is usually used
26// instead.
27//
28_OWLFUNC(int)
30{
31 static const tchar defCaption[] = _T("Unhandled Exception");
32 static const tchar defError[] = _T("Unknown Exception");
33
34 tstring msg = x.why();
35 if (msg.length() == 0)
36 msg = defError;
37
38 if (canResume)
39 {
40 msg += _T("\n\n");
41 msg += canResume;
42 }
43
44 const int flags = MB_ICONSTOP | MB_TOPMOST | MB_TASKMODAL | (canResume ? MB_YESNO : MB_OK);
45 const int r = ::MessageBox(nullptr, msg.c_str(), caption ? caption : defCaption, flags);
46 return (r == IDYES) ? 0 : -1;
47}
48
49//----------------------------------------------------------------------------
50
51/// \class TXOwl
52/// TXOwl is a parent class for several classes designed to describe exceptions. In
53/// most cases, you derive a new class from TXOwl instead of using this one
54/// directly. The ObjectWindows classes derived from TXOwl include
55/// - \c \b TXCompatibility Describes an exception that occurs if TModule::Status is not
56/// zero
57/// - \c \b TXValidator Describes an exception that occurs if there is an invalid validator
58/// expression
59/// - \c \b TXWindow Describes an exception that results from trying to create an invalid
60/// window
61/// - \c \b TXGdi Describes an exception that results from creating an invalid GDI object
62/// - \c \b TXInvalidMainWindow Describes an exception that results from creating an invalid
63/// main window
64/// - \c \b TXMenu Describes an exception that occurs when a menu object is invalid
65/// - \c \b TXInvalidModule Describes an exception that occurs if a TModule object is
66/// invalid
67/// - \c \b TXPrinter Describes an exception that occurs if a printer device context is
68/// invalid
69/// - \c \b TXOutOfMemory Describes an exception that occurs if an out of memory error
70/// occurs
71///
72/// Each of the exception classes describes a particular type of exception. When
73/// your program encounters a given situation that's likely to produce this
74/// exception, it passes control to the specified exception-handling object. If you
75/// use exceptions in your code, you can avoid having to scatter error-handling
76/// procedures throughout your program.
77///
78/// To create an exception handler, place the keyword try before the block of code
79/// that might produce the abnormal condition (the code that might generate an
80/// exception object) and the keyword catch before the block of code that follows
81/// the try block. If an exception is thrown within the try block, the classes
82/// within each of the subsequent catch clauses are checked in sequence. The first
83/// one that matches the class of the exception object is executed.
84///
85/// The following example from MDIFILE.CPP, a sample program on BC5.0x distribution
86/// disk, shows how to set up a try/catch block around the code that might throw an
87/// exception.
88/// \code
89/// void TMDIFileApp::CmRestoreState()
90/// {
91/// char* errorMsg = 0;
92/// ifpstream is(DskFile);
93/// if (is.bad())
94/// errorMsg = "Unable to open desktop file.";
95/// // try block of code //
96/// else {
97/// if (Client->CloseChildrenParen) {
98/// try {
99/// is >>* this;
100/// if (is.bad())
101/// errorMsg = "Error reading desktop file.";
102/// else
103/// Client->CreateChildren();
104/// }
105/// // catch block of code //
106/// catch (xalloc) {
107/// Client->CloseChildren();
108/// errorMsg = "Not enough memory to open file.";
109/// }
110/// }
111/// }
112/// if (errorMsg)
113/// MainWindow->MessageBox(errorMsg, "Error",
114/// MB_OK | MB_ICONEXCLAMATION);
115/// }
116/// \endcode
117
118
119//
120/// An OWL exception with a given message for displaying and an unsigned Id
121/// that can be used for identification or loading a string
122//
124:
125 TXBase(msg),
126 ResId(resId)
127{
128}
129
130//
131/// An OWL exception with a given unsigned Id that can is used for loading a
132/// message string & identification
133//
134/// Loads the string resource identified by the resId parameter and uses this resId
135/// to initialize the TXBase object.
136//
138:
139 TXBase(ResourceIdToString(nullptr, resId, module)),
140 ResId(resId)
141{
142}
143
144//
145/// Destroys a TXOwl object.
146//
148{
149}
150
151//
152/// Called when an unhandled exception is caught at the main message loop level.
153//
154int
159
160TXOwl*
162{
163 return new TXOwl(*this);
164}
165
166//
167/// Throws the exception object. Throw must be implemented in any class derived from
168/// TXOwl.
169//
170void
172{
173 throw *this;
174}
175
176//
177// Construct a TXOwl exception from scratch, and throw it. Two versions
178// corresponding to the two constructor signatures
179//
180void
182{
183 TXOwl(msg, resId).Throw();
184}
185
186//
187//
188//
189void
191{
192 TXOwl(resId, module).Throw();
193}
194
195//
196/// Static member function used to convert a resource id to a 'string'. This
197/// is necessary since we must pass a string to the xmsg base class
198/// constructor. Sets found to true if the resource was located, otherwise
199/// false. In either case, the string is initialized to something
200/// printable.
201/// If the string message cannot be loaded, returns a "not found" message.
202//
205{
206 tchar buf[128];
207
208 bool status = module && module->LoadString(resId, buf, COUNTOF(buf));
209 if (found)
210 *found = status;
211
212 if (!status)
213 _stprintf(buf, _T("Exception #%u (Could not load description string; <owl/except.rc> not bound?)."), resId);
214
215 tstring rscStr(buf);
216 return rscStr;
217}
218
219//
220/// Extension to string loader adds the feature of sprintf'ing an
221/// additional information string into the resource message string.
222//
225{
226 tstring rscMsg = ResourceIdToString(nullptr, resId, module);
227 tchar buf[255];
228 _stprintf(buf, rscMsg.c_str(), infoStr);
229 return tstring(buf);
230}
231
234{
235 return MakeMessage(resId, infoStr.c_str(), module);
236}
237
238//
239/// This extension to the string loader adds the feature of sprintf'ing an
240/// additional information string into the resource message string.
241//
244{
245 tstring rscMsg = ResourceIdToString(nullptr, resId, module);
246 tchar buf[255];
247 _stprintf(buf, rscMsg.c_str(), infoNum);
248 return tstring(buf);
249}
250#if defined(UNICODE)
253{
256 tchar buf[255];
257 _stprintf(buf, rscMsg.c_str(), _A2W(infoStr));
258 return tstring(buf);
259}
260#endif
261//----------------------------------------------------------------------------
262
263//
264/// Constructs a TXOutOfMemory object.
265//
271
274{
275 return new TXOutOfMemory(*this);
276}
277
278//
279/// Throws the exception object. Throw must be implemented in any class derived from
280/// TXOwl.
281//
282void
284{
285 throw *this;
286}
287
288//
289/// Construct a TXOutOfMemory exception from scratch, and throw it
290//
291void
293{
294 TXOutOfMemory().Throw();
295}
296//
297//
298//
304
307{
308 return new TXNotSupportedCall(*this);
309}
310
311//
313{
314 throw *this;
315}
316//
321
322
323///
324} // OWL namespace
325/* ========================================================================== */
326
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
Derived from xmsg, TXBase is the base class for ObjectWindows and ObjectComponents exception-handling...
Definition exbase.h:41
Describes an exception that results from running out of memory.
Definition except.h:78
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _stprintf
Definition cygwin.h:88
#define _T(x)
Definition cygwin.h:51
TXOutOfMemory()
Constructs a TXOutOfMemory object.
Definition except.cpp:266
void Throw()
Throws the exception object.
Definition except.cpp:171
static void Raise()
Definition except.cpp:317
static void Raise()
Construct a TXOutOfMemory exception from scratch, and throw it.
Definition except.cpp:292
static void Raise(const tstring &msg, uint resId=0)
Definition except.cpp:181
static tstring MakeMessage(uint resId, const tstring &infoStr, TModule *module=&GetGlobalModule())
Definition except.cpp:233
static tstring ResourceIdToString(bool *found, uint resId, TModule *module=&GetGlobalModule())
Static member function used to convert a resource id to a 'string'.
Definition except.cpp:204
virtual ~TXOwl()
Destroys a TXOwl object.
Definition except.cpp:147
TXOwl(const tstring &msg, uint resId=0)
An OWL exception with a given message for displaying and an unsigned Id that can be used for identifi...
Definition except.cpp:123
void Throw()
Throws the exception object.
Definition except.cpp:283
TXOutOfMemory * Clone()
Definition except.cpp:273
TXOwl * Clone()
Definition except.cpp:161
TXNotSupportedCall * Clone()
Definition except.cpp:306
virtual int Unhandled(TModule *appModule, uint promptResId)
Per-exception class unhandled-handler, will default to the per-module unhandled-handler.
Definition except.cpp:155
#define _A2W(lpw)
Definition memory.h:220
#define _USES_CONVERSION
Definition memory.h:217
Definition of class TModule.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
int HandleGlobalException(owl::TXBase &x, LPCTSTR caption, LPCTSTR canResume=nullptr)
Definition except.cpp:29
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
ObjectWindows exception class & function definitions.
#define _OWLFUNC(p)
Definition defs.h:341
Base window class TWindow definition, including HWND encapsulation.