views:

199

answers:

3

I am trying to start an error-reporting activty if unhandled exception detected. The problem is with exceptions thrown from main thread. Is there any way to start an activity if main thread crashed?

A: 

I think this is the wrong way to go about it. What you need to do is make sure you catch those exceptions, and pop up an error-reporting activity when you catch them.

mbaird
+1  A: 

The purpose of handling an exception is to catch and handle that exception in your own way (like starting an Activity), opposed to Android's way of crashing the main thread.

If you catch the exception, you can avoid having the thread crash.

try {
// your code here
} catch (Exception e) { // catches all exceptions
// log to a file
// start your Activity
}
Anthony Forloney
I am talking about *unexpected exceptions* that I can't predict upfront, but they still can happen (some bug in code). And I would like to display some activity in this case, instead of simple "force close". One way to go is to add try/catch to every method invoked by main-thread, but this is not handy. I would prefer to have singe UncaughtExceptionHandler and use try/catch only for expected exceptions.
alex2k8
That sounds like a great idea, I would go along with that.
Anthony Forloney
+1  A: 

The approach I've seen used for error catching in an UncaughtExcpetionHandler is to write the crash data out to file and then start the error handling Activity when the application is restarted based on the existence of the crash data file.

Depending on what you want your Activity to do, this might work for you.

Dave Webb