Phonegap and catching global android exceptions
I'm creating a phonegap application and would like to prevent it from crashing. I thought that if I could find a way to manage exceptions on a global level, I could catch the exception and just ignore it. However, so far my error catching code doesn't seem to pick it up.
Anyway idea of what I am doing wrong? Is there a different method I should try?
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public 开发者_如何学JAVAvoid uncaughtException(Thread thread, Throwable ex) {
Log.e("uncaught error",ex.getMessage());
Log.e("uncaught error",ex.getStackTrace().toString());
}
});
Actualy, this doesn't sound like a great idea.
Exception mechanism has been design to answer to each exception individually. Of course, you can fine tune it and have very special catch clause for each exception type, or use a coarser approach and group exceptions together using inheritance.
But having a single catch clause for everything that is happening is not a very good idea. Although there is one case in which I would recommend this pattern : for a server that needs to be particularly robust and always up and running, able to recover from whatever situation.
But for an android app, no.
So, I would recommend you to watch carefully where your code is throwing an exception through logcat, and patch every trouble your program faces. And if you are looking for a way to, at some place and not in the whole program, catch whatever is thrown, use the Throwable interface, super class (interface) of all that can be thrown.
try
{
//your code
}//try
catch( Throwable t)
{
t.printStackTrace();
}//catch
Regards, Stéphane
精彩评论