In my Application class I am trying to catch a force close before it happens, so I can log it and then rethrow it so the android can handle it. I do this since some users do not report force closes.
I am developing in eclipse, and eclipse is not allowing me to rethrow the exception. It shows an error saying “Unhandled exception type Throwable: Surround with try/catch”. How can I rethrow the exception?
public class MainApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
try
{
//Log exception before app force closes
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
"Errors", // Category
"MainActivity", // Action
"Force Close: "+ex.toString(), // Label
0); // Value
AnalyticsUtils.getInstance(MainApplication.this).dispatch();
Toast.makeText(MainApplication.this, "Snap! Something broke. Please report the Force Close so I can fix it.", Toast.LENGTH_LONG);
//rethrow the Exception so user can report it
//throw ex; //<-- **eclipse is showing an error to surround with try/catch**
}
});
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Apologies, not an Android expert – but looks like you can’t throw ex because your method signature “void uncaughtException(Thread, Throwable)” doesn’t declare that it “throws” anything.
Assuming you’re overriding an API interface and (a) can’t modify this signature and (b) don’t want to because you’d be throwing it out of context, could you instead use a decorator pattern and basically subclass the default UncaughtExceptionHandler implementation to log your message and then let it carry on processing as usual?
Edit: untested, but this might look a bit like: