How to catch a panic
Yes, a panic. Although panics, by their nature, are uncatchable there is a way to prevent them from severely influencing your program.
First off, why would you do it? What is the use-case? Typically a panic draws attention to a programmatic error that should have already been corrected in the program. It always terminates the program execution and it's on purpose: programs panic from an unrecoverable stage. So why would we want to catch a panic if we were able to do so?
Well, there is at least one special use-case: in a test framework used for automated testing. In a typical unit testing framework, for example, where test suites containing test cases are being performed it's always a nice feature if the execution of tests doesn't get interrupted even in case of a fatal error. If the severe programming error doesn't harm other test cases, then it's ideal if the termination of a test case doesn't influence the life-time of other test cases.
Can panics be caught? At first sight, it may seem that User::SetExceptionHandler() would do. But it doesn't: exceptions are not the same as panics. So installing a new exception handler will not enable us to catch panics, even though it makes it possible to catch a small portion of ALL panics (e.g. an access violation aka KERN-EXE 3).
The ultimate solution is to let your test case run in a new thread. A new thread that you have control over: you create, destroy and most importantly monitor it. You can request notification when this thread dies normally or otherwise via RThread::Logon(). Combined this with RThread::ExitReason(), you can get a clue when and how a thread has exited. Even in case of a panic your main thread will not be affected at all so your - test - framework can continue to run.
The following example shows a console based exe which monitors other thread's death, displaying type and reason:
class CThreadNotifier : public CActive
TInt RunError(TInt aError);
LOCAL_D CConsoleBase* console;
__ASSERT_ALWAYS(!IsActive(), User::Panic(KPanicMsg, 0));
if (iStatus == KErrDied)
console->Printf(_L("Thread %S (%d) died (Type: %d, reason %d)\n"),
&thread.Name(), (int)thread.Id(), thread.ExitType(), thread.ExitReason());
TInt CThreadNotifier::RunError(TInt /*aError*/)
LOCAL_C void callExampleL()
console = Console::NewL(_L("Thread Notifier"), TSize(KDefaultConsWidth,
CActiveScheduler* scheduler = new(ELeave) CActiveScheduler;
CThreadNotifier* notifier = new(ELeave) CThreadNotifier;
CleanupStack::PopAndDestroy(3, console); // console, scheduler, notifier
GLDEF_C TInt E32Main()
CTrapCleanup* cleanup = CTrapCleanup::New();
__ASSERT_ALWAYS(!error, User::Panic(KPanicMsg, error));