Freitag, 19. Februar 2010

Print stracktrace to BlackBerry eventlog

The BlackBerry Eventlog (accessable via ALT-LGLG on most devices) is great.
An application can log everything it wants starting from errors up to debug information. And: for uncaught exceptions a full stacktrace is written! Every wanted to use this in your app as well?

It isn't possible to access the stacktrace of an exception. ME does not know "getStackTrace()" there is only "printStackTrace()" which write to console only available for the exception classes. Also ME does not allow to redirect System.err or System.out to get to this information.
The only chance to see the stacktrace of a caught exception is to use printStackTrace() while in attached debugging mode.

But there is a nice trick to print the stacktrace directly to the eventlog: catch Throwable:

try
{
return instance.doAction();
}
catch(Throwable exc)
{
return false;
}

If an exceptions in doAction() occurs (as our best friend NullPointerException) it will be written by the system directly to eventlog. This won't happen if you catch Exception or RuntimeExcepion or more specific one.

1 Kommentar: