-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcept.cpp
More file actions
587 lines (418 loc) · 14.8 KB
/
Except.cpp
File metadata and controls
587 lines (418 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
Copyright (C) 1998-2007 Emil Maskovsky
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
@file
Exception support (implementation).
@author Emil Maskovsky
*/
/* System libraries. */
/* Framework libraries. */
#include "mx/StdStrm.hpp"
#include "mx/App/App.hpp"
/* Application specific. */
#include "mx/Except.hpp"
#ifndef MXCPP_FIX_USE_OLD_C_HEADERS
#include <typeinfo>
#include <exception>
#ifndef MXCPP_FIX_HAS_NOT_STD_NAMESPACE
using namespace std;
#endif
#else // MXCPP_FIX_USE_OLD_C_HEADERS
#include <typeinfo.h>
#ifndef MXCPP_FIX_EH_UNSUPPORTED
#include <eh.h>
#endif
#endif // MXCPP_FIX_USE_OLD_C_HEADERS
/**
@page exceptions C++ Exception handling
We support C++ Exceptions handling, it seems that it is supported by
wide-range compilers, and C++ Exception usage is very convenient.
@note
We support even uncaught exception handler (exceptions that were not caught
in the application); nearly all compilers look as they support it (have
required functions defined), but it seems that only Digital Mars C++ compiler
supports this functionality correctly for now.
The default application catches all, even weird, exceptions (and derived
application @b should do it too, if reimplemented), so the uncaught exception
handler functionality is not critical. But, if you want to be absolutely
sure, you might want to use the compiler which suports it.
Additional information:
@li @subpage exceptions_implementation
@section exceptions_cpp Compilers that can support uncaught exception handlers
@li @ref build_dmc - 32-bit Windows, 16-bit DOS
*/
/**
@page exceptions_implementation Exceptions
All framework exceptions are based on mx::Exception class.
This base class extends the standard std::exception class (for compilers,
which support it), to allow usage of abstract catch clause to catch all
possible exceptions.
@section exceptions_throwing Throwing exceptions
If you want to use the @ref exceptions_handling "extended exception hadling"
provided by the framework, you should use the construct mxThrow() to throw
an exception.
You can however use any way of throwing exceptions, which is possible in
C++ (see http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6 for
further details), and even mix them (but this is very strongly discouraged).
Next example shows how to throw/catch any mx::Exception based exception by
pointer or reference:
@include ExceptionPointerOrReference.cpp
@section exceptions_handling Framework extended exception handling
The framework provides some special exception handling techniques, which are
implemented by @ref exceptions_throwing "special exception constructs".
The key benefits of usage of these constructs are following:
@li @b File-Line @b tracking: You will be able to gain the filename and
line, where the exception was originated (thrown).
@section exceptions_user Declare user exceptions
The framework user can declare its own exception classes based on
mx::Exception. Application exception should extend the
mx::ApplicationException class, only in special cases you might want to
extend mx::SystemException.
All exceptions derived from mx::Exception should be declared using
MX_DECLARE_EXCEPTION_CLASS() and MX_IMPLEMENT_EXCEPTION_CLASS() macros,
otherwise some extensions used in exception handling (e.g. @ref RTTI) will
not work correctly.
*/
// Start the exception implementation.
MX_IMPLEMENT_EXCEPTION_CLASS(mx::Exception);
/**
Destructor.
*/
/* virtual */ mx::Exception::~Exception()
{}
namespace mx
{
/**
The exception failure handler.
@tparam ExceptionType The exception type.
*/
template< class ExceptionType >
class FailureHandler
{
MX_CLASS_NO_COPY(FailureHandler);
MX_CLASS_NO_ASSIGNMENT(FailureHandler);
// Construction, destruction.
public:
/**
Constructor taking the exception pointer.
@param [in] pException The exception pointer.
*/
explicit inline FailureHandler(const ExceptionType * const pException)
: m_pException(pException)
{}
// Class instance methods.
public:
/**
Normal handling of the exception.
Designed for handling of expected exceptions.
@param [in] bDestroy Destroy the exception after handling.
This handler does not return, always causes the application shutdown.
*/
MX_NORETURN HandleFailure(const bool bDestroy)
{
ProcessErrors(bDestroy);
exit(Application::RC_FAILURE);
}
/**
Handling of unexpected exception.
Designed for handling of unexpected (uncaught) exceptions.
@param [in] bDestroy Destroy the exception after handling.
This handler does not return, always causes the application shutdown via
the standard @c abort() function.
*/
MX_NORETURN HandleUncaughtException(const bool bDestroy)
{
mxLogTrace(Log::TRACE_Exception, Log::LEVEL_Highest,
_("Unexpected termination handler entered!"));
ProcessErrors(bDestroy, true);
abort();
}
private:
/**
Process the exception handling.
@param [in] bDestroy Destroy the exception after handling.
@param [in] bUnhandled Handling of unexpected exception (optional).
@return
Returns the @ref logging_macros "result of logging" the exception
message.
*/
Size ProcessErrors(const bool bDestroy, const bool bUnhandled = false)
{
if (!m_pException)
{
return mxLogFatalError(_("Weird exception caught!"));
}
// Otherwise provide the user with the most detailed information about
// the exception we can get.
if (bUnhandled)
{
mxLogError(_("Unhandled exception caught!"));
}
const Size iReturnCode = Exception::GlobalLogMessage(*m_pException);
if (bDestroy)
{
delete const_cast< ExceptionType * >(m_pException);
}
return iReturnCode;
}
// Class instance attributes.
private:
/// The exception which is being handled.
const ExceptionType * const m_pException;
}; // class FailureHandler< ... >
/**
Default logging of the exception message.
@param [in] sExceptionName The name of the exception.
@param [in] sMessage The exception message (may be @c NULL).
@param [in] xFileInfo Source file location information.
@param [in] iLogType Type of log message.
@return
Returns the @ref logging_macros "result of logging" the exception message.
*/
static Size doLogMessage(
const char * const sExceptionName,
const Char * sMessage,
const Debug::Checkpoint & xFileInfo,
const Log::LogType iLogType)
{
#ifndef MXCPP_UNICODE
const char * const sClassName = sExceptionName;
#else
Char sClassName[64];
mbstowcs(sClassName, sExceptionName, sizeof(sClassName) - 1);
#endif
mxAssert(sExceptionName != NULL);
return Log(iLogType, xFileInfo).LogMessage(
#ifndef MXCPP_UNICODE
_("Exception '%s' caught%s%s")
#else
_("Exception '%ls' caught%ls%ls")
#endif
, sClassName,
sMessage ? _(" with message: ") : _T(""),
sMessage ? sMessage : _T(""));
}
} // namespace mx
/**
Uncaught exception handler.
Handles the exception and terminates the application using standard function
@c abort().
@param [in] pException The exception pointer.
*/
/* static */ MX_NORETURN mx::Exception::HandleUncaughtException(
const Exception * const pException)
{
FailureHandler< Exception >(pException).HandleUncaughtException(false);
}
/**
Exception failure handler.
Handles the exception and terminates the application.
@param [in] pException The exception pointer.
*/
/* static */ MX_NORETURN mx::Exception::HandleFailure(
const Exception * const pException)
{
FailureHandler< Exception >(pException).HandleFailure(false);
}
/**
Exception failure handler (the std::exception variant).
Handles the exception and terminates the application.
@param [in] pException The exception pointer.
*/
/* static */ MX_NORETURN mx::Exception::HandleFailure(
const std::exception * const pException)
{
FailureHandler< std::exception >(pException).HandleFailure(false);
}
/**
Handle the exception failure and destroy the exception.
This handler is to be used along with exceptions, which are thrown and caught
by pointer.
@param [in] pException The exception pointer.
@warning
Should not be used with exceptions which are not dynamically allocated using
the @c new operator! In the case of statically allocated exceptions, use
Exception::HandleFailure() instead!
@see Exception::HandleFailure()
*/
/* static */ MX_NORETURN mx::Exception::FailAndDestroy(
const Exception * const pException)
{
FailureHandler< Exception >(pException).HandleFailure(true);
}
/**
Handle the exception failure and destroy the exception
(the std::exception variant).
This handler is to be used along with exceptions, which are thrown and caught
by pointer.
@param [in] pException The exception pointer.
@warning
Should not be used with exceptions which are not dynamically allocated using
the @c new operator! In the case of statically allocated exceptions, use
Exception::HandleFailure() instead!
@see Exception::HandleFailure()
*/
/* static */ MX_NORETURN mx::Exception::FailAndDestroy(
const std::exception * const pException)
{
FailureHandler< std::exception >(pException).HandleFailure(true);
}
/**
Log the message of an exception.
@param [in] pException The exception instance.
@param [in] iLogType Type of log message.
@return
Returns the @ref logging_macros "result of logging" the exception message.
*/
/* static */ mx::Size mx::Exception::GlobalLogMessage(
const Exception & pException,
const Log::LogType iLogType)
{
return doLogMessage(pException.getName(), pException.message(),
pException.m_xFileLocation, iLogType);
}
/**
Log the message of an exception (the std::exception variant).
@param [in] pException The exception instance.
@param [in] iLogType Type of log message.
@return
Returns the @ref logging_macros "result of logging" the exception message.
*/
/* static */ mx::Size mx::Exception::GlobalLogMessage(
const std::exception & pException,
const Log::LogType iLogType)
{
#ifndef MXCPP_UNICODE
const char * const sMessage = pException.what();
#else
Char sMessage[128];
mbstowcs(sMessage, pException.what(), sizeof(sMessage) - 1);
#endif
return doLogMessage(Class::FilterTypeName(typeid(pException).name()),
sMessage,
// The file information is empty.
Debug::Checkpoint(), iLogType);
}
/**
Get the message associated with the exception.
*/
/* MX_OVERRIDDEN */ const char * mx::Exception::what() const
{
/*
const Char * const sMessage = message();
if (sMessage)
{
// Use the associated message, if any set.
return sMessage;
}
*/
// Otherwise, show the name of the exception.
return getName();
}
/**
Log the exception message.
@param [in] iLogType Type of log message.
@return
Returns the @ref logging_macros "result of logging" the exception message.
*/
mx::Size mx::Exception::LogMessage(const Log::LogType iLogType) const
{
return GlobalLogMessage(*this, iLogType);
}
/**
Handle the exception failure.
Logs the exception message via LogMessage() and terminates the application.
*/
MX_NORETURN mx::Exception::Fail() const
{
LogMessage();
exit(mx::Application::RC_FAILURE);
}
// Start the exception implementation.
MX_IMPLEMENT_EXCEPTION_CLASS(mx::SystemException);
// Start the exception implementation.
MX_IMPLEMENT_EXCEPTION_CLASS(mx::KernelException);
// Start the exception implementation.
MX_IMPLEMENT_EXCEPTION_CLASS(mx::MemoryException);
// Start the exception implementation.
MX_IMPLEMENT_EXCEPTION_CLASS(mx::IOException);
namespace mx
{
// Uncaught (aka default) exception handler.
// Declared only internaly (cannot be used outside).
/**
Helper class for setting the default exception handlers.
*/
class UncaughtExceptionHandler
{
MX_CLASS_NO_COPY(UncaughtExceptionHandler);
MX_CLASS_NO_ASSIGNMENT(UncaughtExceptionHandler);
// Class attributes (static).
private:
static const UncaughtExceptionHandler sm_xInitializer;
// Class functions (static).
private:
static inline MX_NORETURN HandleTerminate();
static inline MX_NORETURN HandleUnexpected();
// Construction, destruction.
public:
UncaughtExceptionHandler();
}; // class UncaughtExceptionHandler
} // namespace mx
/**
Instance of UncaughtExceptionHandler to enforce invocation of its
constructor.
*/
const mx::UncaughtExceptionHandler
mx::UncaughtExceptionHandler::sm_xInitializer;
/// Signature of the @project to appear in executables.
static const char mxCppFrameworkSignature[]
= "This application uses Demo C++ Framework,"
" (C) 1996-2008 Emil Maskovsky";
/**
Initialize exception handlers.
*/
mx::UncaughtExceptionHandler::UncaughtExceptionHandler()
{
// Use the signature to be always contributed in the output.
Use(mxCppFrameworkSignature);
#ifndef MXCPP_FIX_EH_UNSUPPORTED
// Cast to the target type needed for using with some compilers
// (e.g. Watcom has problems with "noreturn" specification).
set_terminate(reinterpret_cast< void (*)() >(&HandleTerminate));
set_unexpected(reinterpret_cast< void (*)() >(&HandleUnexpected));
#endif // MXCPP_FIX_EH_UNSUPPORTED
}
/**
Our handler for uncaught exceptions.
*/
/* static */ inline MX_NORETURN mx::UncaughtExceptionHandler::HandleTerminate()
{
HandleUnexpected();
}
/**
Our handler for unexpected exceptions.
*/
/* static */ inline MX_NORETURN mx::UncaughtExceptionHandler::HandleUnexpected()
{
Exception::HandleUncaughtException(
/* Exception::getLastRaisedException() */);
// Disabled to detect last raised exception for now - can have problems
// if RTTI does not work.
}
// Define inline methods here if inlining is disabled.
#ifndef MX_INLINE_ENABLED
#include "mx/Except.inl"
#endif
/* EOF */