Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why use UncaughtExceptionHandler?

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

Gentlemen, as we know, there is an UncaughtExceptionHandler, which can be introduced into the thread:



Could please explain, why this approach is more preferrable than just introducing a try-catch block which surrounds everything inside the run() method?

 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe, UncaughtExceptionHandler was introduced in JDK1.5, hence, prior to that the try-catch-finally was the only approach available.

Now that it is available from JDK1.5 onwards, you may use it to write compact code.
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using try-catch block you handle the exception the way you want and probably you can re execute same job or can continue processing your code in same thread. But this is not case with UncaughtExceptionHandler. If you look at the javadoc for UncaughtExceptionHandler, uncaughtException() method will be executed when thread is abruptly terminated and you can't recover it. Probably you can use this method for auditing purpose. And may be after that your thread will be stopped.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the most important use is for threads for which you don't implement the Runnable yourself -- for example, when you're implementing a container or framework of some kind.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic