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#if defined(BI_MSG_LANGUAGE) && BI_MSG_LANGUAGE == 0x0411
32 static const char defCaption[] = "未処理の例外";
33 static const char defError[] = "?知I預SO";
34#else
35 static const tchar defCaption[] = _T("Unhandled Exception");
36 static const tchar defError[] = _T("Unknown Exception");
37#endif
38
39 tstring msg = x.why();
40 if (msg.length() == 0)
41 msg = defError;
42
43 if (canResume)
44 {
45 msg += _T("\n\n");
46 msg += canResume;
47 }
48
49 const int flags = MB_ICONSTOP | MB_TOPMOST | MB_TASKMODAL | (canResume ? MB_YESNO : MB_OK);
50 const int r = ::MessageBox(nullptr, msg.c_str(), caption ? caption : defCaption, flags);
51 return (r == IDYES) ? 0 : -1;
52}
53
54//----------------------------------------------------------------------------
55
56/// \class TXOwl
57/// TXOwl is a parent class for several classes designed to describe exceptions. In
58/// most cases, you derive a new class from TXOwl instead of using this one
59/// directly. The ObjectWindows classes derived from TXOwl include
60/// - \c \b TXCompatibility Describes an exception that occurs if TModule::Status is not
61/// zero
62/// - \c \b TXValidator Describes an exception that occurs if there is an invalid validator
63/// expression
64/// - \c \b TXWindow Describes an exception that results from trying to create an invalid
65/// window
66/// - \c \b TXGdi Describes an exception that results from creating an invalid GDI object
67/// - \c \b TXInvalidMainWindow Describes an exception that results from creating an invalid
68/// main window
69/// - \c \b TXMenu Describes an exception that occurs when a menu object is invalid
70/// - \c \b TXInvalidModule Describes an exception that occurs if a TModule object is
71/// invalid
72/// - \c \b TXPrinter Describes an exception that occurs if a printer device context is
73/// invalid
74/// - \c \b TXOutOfMemory Describes an exception that occurs if an out of memory error
75/// occurs
76///
77/// Each of the exception classes describes a particular type of exception. When
78/// your program encounters a given situation that's likely to produce this
79/// exception, it passes control to the specified exception-handling object. If you
80/// use exceptions in your code, you can avoid having to scatter error-handling
81/// procedures throughout your program.
82///
83/// To create an exception handler, place the keyword try before the block of code
84/// that might produce the abnormal condition (the code that might generate an
85/// exception object) and the keyword catch before the block of code that follows
86/// the try block. If an exception is thrown within the try block, the classes
87/// within each of the subsequent catch clauses are checked in sequence. The first
88/// one that matches the class of the exception object is executed.
89///
90/// The following example from MDIFILE.CPP, a sample program on BC5.0x distribution
91/// disk, shows how to set up a try/catch block around the code that might throw an
92/// exception.
93/// \code
94/// void TMDIFileApp::CmRestoreState()
95/// {
96/// char* errorMsg = 0;
97/// ifpstream is(DskFile);
98/// if (is.bad())
99/// errorMsg = "Unable to open desktop file.";
100/// // try block of code //
101/// else {
102/// if (Client->CloseChildrenParen) {
103/// try {
104/// is >>* this;
105/// if (is.bad())
106/// errorMsg = "Error reading desktop file.";
107/// else
108/// Client->CreateChildren();
109/// }
110/// // catch block of code //
111/// catch (xalloc) {
112/// Client->CloseChildren();
113/// errorMsg = "Not enough memory to open file.";
114/// }
115/// }
116/// }
117/// if (errorMsg)
118/// MainWindow->MessageBox(errorMsg, "Error",
119/// MB_OK | MB_ICONEXCLAMATION);
120/// }
121/// \endcode
122
123
124//
125/// An OWL exception with a given message for displaying and an unsigned Id
126/// that can be used for identification or loading a string
127//
129:
130 TXBase(msg),
131 ResId(resId)
132{
133}
134
135//
136/// An OWL exception with a given unsigned Id that can is used for loading a
137/// message string & identification
138//
139/// Loads the string resource identified by the resId parameter and uses this resId
140/// to initialize the TXBase object.
141//
143:
144 TXBase(ResourceIdToString(nullptr, resId, module)),
145 ResId(resId)
146{
147}
148
149//
150/// Destroys a TXOwl object.
151//
153{
154}
155
156//
157/// Called when an unhandled exception is caught at the main message loop level.
158//
159int
164
165TXOwl*
167{
168 return new TXOwl(*this);
169}
170
171//
172/// Throws the exception object. Throw must be implemented in any class derived from
173/// TXOwl.
174//
175void
177{
178 throw *this;
179}
180
181//
182// Construct a TXOwl exception from scratch, and throw it. Two versions
183// corresponding to the two constructor signatures
184//
185void
187{
188 TXOwl(msg, resId).Throw();
189}
190
191//
192//
193//
194void
196{
197 TXOwl(resId, module).Throw();
198}
199
200//
201/// Static member function used to convert a resource id to a 'string'. This
202/// is necessary since we must pass a string to the xmsg base class
203/// constructor. Sets found to true if the resource was located, otherwise
204/// false. In either case, the string is initialized to something
205/// printable.
206/// If the string message cannot be loaded, returns a "not found" message.
207//
210{
211 tchar buf[128];
212
213 bool status = module && module->LoadString(resId, buf, COUNTOF(buf));
214 if (found)
215 *found = status;
216
217 if (!status)
218#if BI_MSG_LANGUAGE == 0x0411
219 sprintf(buf, "例外 #%u (メッセージが用意されていません. , resId);
220#else
221 _stprintf(buf, _T("Exception #%u (Could not load description string; <owl/except.rc> not bound?)."), resId);
222#endif
223
224 tstring rscStr(buf);
225 return rscStr;
226}
227
228//
229/// Extension to string loader adds the feature of sprintf'ing an
230/// additional information string into the resource message string.
231//
234{
235 tstring rscMsg = ResourceIdToString(nullptr, resId, module);
236 tchar buf[255];
237 _stprintf(buf, rscMsg.c_str(), infoStr);
238 return tstring(buf);
239}
240
243{
244 return MakeMessage(resId, infoStr.c_str(), module);
245}
246
247//
248/// This extension to the string loader adds the feature of sprintf'ing an
249/// additional information string into the resource message string.
250//
253{
254 tstring rscMsg = ResourceIdToString(nullptr, resId, module);
255 tchar buf[255];
256 _stprintf(buf, rscMsg.c_str(), infoNum);
257 return tstring(buf);
258}
259#if defined(UNICODE)
262{
265 tchar buf[255];
266 _stprintf(buf, rscMsg.c_str(), _A2W(infoStr));
267 return tstring(buf);
268}
269#endif
270//----------------------------------------------------------------------------
271
272//
273/// Constructs a TXOutOfMemory object.
274//
280
283{
284 return new TXOutOfMemory(*this);
285}
286
287//
288/// Throws the exception object. Throw must be implemented in any class derived from
289/// TXOwl.
290//
291void
293{
294 throw *this;
295}
296
297//
298/// Construct a TXOutOfMemory exception from scratch, and throw it
299//
300void
302{
303 TXOutOfMemory().Throw();
304}
305//
306//
307//
313
316{
317 return new TXNotSupportedCall(*this);
318}
319
320//
322{
323 throw *this;
324}
325//
330
331
332///
333} // OWL namespace
334/* ========================================================================== */
335
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:275
void Throw()
Throws the exception object.
Definition except.cpp:176
static void Raise()
Definition except.cpp:326
static void Raise()
Construct a TXOutOfMemory exception from scratch, and throw it.
Definition except.cpp:301
static void Raise(const tstring &msg, uint resId=0)
Definition except.cpp:186
static tstring MakeMessage(uint resId, const tstring &infoStr, TModule *module=&GetGlobalModule())
Definition except.cpp:242
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:209
virtual ~TXOwl()
Destroys a TXOwl object.
Definition except.cpp:152
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:128
void Throw()
Throws the exception object.
Definition except.cpp:292
TXOutOfMemory * Clone()
Definition except.cpp:282
TXOwl * Clone()
Definition except.cpp:166
TXNotSupportedCall * Clone()
Definition except.cpp:315
virtual int Unhandled(TModule *appModule, uint promptResId)
Per-exception class unhandled-handler, will default to the per-module unhandled-handler.
Definition except.cpp:160
#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.