• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InterruptedException handling

 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you tony. That answers my question.

I think once i am done with the tutorial topic, my immediate next stop should be JLS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic