Monday, 5 December 2016

What happens when an Exception occurs in a thread ?

The most important question usually asked in interview about what happen if thread throws Exception.

If Exception is not caught then thread will die.It will prints the Exception to the console.The Thread itself will exit at this point, it couldn't continue anyway, because its run() method has finished.

So if you want the exception to be re-raised in your main thread, you can define an UncaughtExceptionHandler. And then call Thread.setUncaughtExceptionHandler on that thread after its created ,passing in your custom Exception handler.

Any uncaught exception from a thread is propagated to the thread's UncaughtExceptionHandler. If there's none defined, it goes to the thread group's handler, if that isn't set either, it goes to the default handler.



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment