aspose file tools*
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
Author

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,
Rakesh
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
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: http://aspose.com/file-tools
 
subject: InterruptedException handling