proper handling of InterruptedException

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

proper handling of InterruptedException

Moran Avigdor
A question arose, your opinions are welcome -

What is considered a proper handling of InterrupedException?
Is it a common practice to throw InterruptedException when the thread
was interrupted while waiting?
or should the thread catch it, ignore it and gracefully die?

Thank you.

--
Moran

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: proper handling of InterruptedException

Joe Bowbeer
If interrupts are used as the primary means of cancellation then I
prefer that blocking methods be interruptible and that all
interruptible methods declare "throws InterruptedException" -- and
that InterruptedExceptions "happen" with as little interference as
possible.

This is simple to implement at the sight of the interrupt and, at
runtime, results in a clear record of where the interrupt was
detected. It also encourages a kind of rigor wrt exceptions and
shutdown that is good for you :-)

Note that even if your method throws InterruptedException, it still
might need to catch the exception before rethrowing it, for example if
some post-interrupt cleanup is needed.

On 10/30/06, Moran Avigdor <[hidden email]> wrote:
> A question arose, your opinions are welcome -
>
> What is considered a proper handling of InterrupedException?
> Is it a common practice to throw InterruptedException when the thread
> was interrupted while waiting?
> or should the thread catch it, ignore it and gracefully die?
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: proper handling of InterruptedException

Brian Goetz
In reply to this post by Moran Avigdor
The cardinal rule is: Never hide the fact that an interruption occurred.

So, your choices are:
  - Throw IE yourself
  - Re-set the interrupted bit (Thread.currentThread().interrupt())
  - Handle the interruption

The classes in JUC (like BlockingQueue and Semaphore) throw IE since
their caller is in a better position to deal with the interruption than
they are.

Lots more detail in JCiP Ch 7.

Moran Avigdor wrote:
> A question arose, your opinions are welcome -
>
> What is considered a proper handling of InterrupedException?
> Is it a common practice to throw InterruptedException when the thread
> was interrupted while waiting?
> or should the thread catch it, ignore it and gracefully die?
>
> Thank you.
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: proper handling of InterruptedException

David Holmes-3
In reply to this post by Moran Avigdor
Just one additional note:

> or should the thread catch it, ignore it and gracefully die?

The only circumstances where a thread can "gracefully die" due to an
exception is in the threasd's own run() method - where a return will cause
the thread to gracefully die. In that one case it is fine to catch it and
gracefully die - you aren't ignoring it as you are dying in response to it.

But any method further down the call chain needs to re-throw or re-assert as
Brian/Joe mentioned.

Cheers,
David Holmes

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest