File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes InterruptedException handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "InterruptedException handling" Watch "InterruptedException handling" New topic

InterruptedException handling

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

When a thread supports InterruptedException, it can stop what it is doing currently and come to an end. This is well documented in java tutorial.

However, when talking about throwing the InterruptedException the documentation says

In more complex applications, it might make more sense to throw an InterruptedException:

if (Thread.interrupted()) {
throw new InterruptedException();

This allows interrupt handling code to be centralized in a catch clause.

Am i correct in saying "in cases where the InterruptedException is thrown, the exception handler will always be part of the same thread"?

Warm Regards,
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Exception handling is executed by the thread that encountered the exception. The JLS says

If no catch clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated. Before termination, all finally clauses are executed and the uncaught exception is handled according to the following rules:

If the current thread has an uncaught exception handler set, then that handler is executed.

Otherwise, the method uncaughtException is invoked for the ThreadGroup that is the parent of the current thread. If the ThreadGroup and its parent ThreadGroups do not override uncaughtException, then the default handler's uncaughtException method is invoke
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Thank you tony. That answers my question.

I think once i am done with the tutorial topic, my immediate next stop should be JLS.
I agree. Here's the link:
subject: InterruptedException handling
It's not a secret anymore!