This week's giveaway is in the Groovy forum.
We're giving away four copies of Groovy Fundamentals video training course and have Ken Kousen on-line!
See this thread for details.
The moose likes Java in General and the fly likes Global Exception Handlers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Global Exception Handlers" Watch "Global Exception Handlers" New topic
Author

Global Exception Handlers

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I am thinking of setting up a global, default Exception handler for my (Android) Mobile application using Thread.setDefaultUncaughtExceptionHandler(...) call. I am thinking of just displaying an Alert Dialog with appropriate message to the user.

Are there any gotchas, caveats and rules that one needs to follow when setting a DefaultExceptionHandlers? Any best practices like making sure that the process is killed, full stack trace is written to logs etc. ?

Links to documentation, tutorials etc. that can throw some light on this are welcome.

Thanks.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19993
    
  25

You don't need to clean up any processes; once the uncaughtException method finishes that thread ends and the JVM will clean up as default. You could print the stack trace if you'd like to but there is no need for it. In the end, you can do whatever you want - print the exception, show a dialog*, write the exception to a log file, a combination of the previous three, anything else - as long as it doesn't throw any exceptions.

* Keep in mind to make sure to show the dialog on the Event Dispatcher Thread!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Thanks Rob!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19993
    
  25

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Global Exception Handlers