Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18913

Re: Interrupted exception chaining

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Interrupted exception chaining
References <d203b18b-61ce-439e-9b1f-9cf056c1d161@googlegroups.com>
Message-ID <ETk8s.3$kz7.0@newsfe02.iad> (permalink)
Date 2012-09-25 09:22 -0700

Show all headers | View raw


On 9/25/12 3:25 AM, raphfrk@gmail.com wrote:
> Is there a recommended way of "chaining" interrupted exceptions?
>
> This is to implement a method call that doesn't throw an interrupted exception, but which calls a method which can be interrupted.
>
> public void uninterruptableWait(Object c) {
>      boolean done = false;
>      boolean interrupted = false;
>      synchronized (c) {
>          while (!done) {
>              try {
>                  c.wait();
>                  done = true;
>              } catch (InterrupedException ie) {
>                  interrupted = true;
>              }
>          }
>      }
>      if (interrupted) {
>          Thread.currentThread().interrupt();
>      }
> }
>
> If that interrupt was unexpected, and causes a stack trace, then it would be nice if it could include the details from the thrown exception.
>
> Is there a better way to do the above?

To take a pattern from Spring Binding (which may have taken it from 
elsewhere), you can keep a list of the caught exceptions, and then at 
the end of your method if that list is not empty, throw a new exception 
which contains the list.

BTW, the stack-trace will only be on the c.wait() line, since that is 
where the interrupted exception will be thrown.  This does not help you 
know who interrupted you unexpectedly.

Do you really have a use-case for uninterruptableWait? Perhaps you 
should instead have a different approach to interrupting that thread. 
An interrupt is often a result of a user-action, and ignoring it will 
make users mad.  It may also be the case that the interrupt was caused 
because something else failed, and waiting no longer is useful.

I'd be curious to read your use case.  I've written similar code in the 
past, and have since realized it was misguided.

Good luck,
Daniel.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Interrupted exception chaining raphfrk@gmail.com - 2012-09-25 03:25 -0700
  Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 08:56 -0400
  Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 08:33 -0700
    Re: Interrupted exception chaining Ivan Ryan <ivan.ryan@gmail.com> - 2012-09-25 09:28 -0700
      Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 13:12 -0400
      Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 12:00 -0700
  Re: Interrupted exception chaining Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-25 09:22 -0700
    Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:26 +0200
      Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 13:37 -0700
        Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:47 +0200
          Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:53 +0200
            Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 22:53 +0200
          Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 14:08 -0700
            Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:23 +0200
              Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:24 +0200
              Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:32 +0200
            Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:42 +0200
              Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:48 +0200
              Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-25 23:55 +0200
                Re: Interrupted exception chaining markspace <-@.> - 2012-09-25 15:33 -0700
      Re: Interrupted exception chaining Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-25 21:43 -0400
  Re: Interrupted exception chaining Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 01:43 +0200
    Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-26 10:24 +0200
      Re: Interrupted exception chaining Jan Burse <janburse@fastmail.fm> - 2012-09-26 10:32 +0200
        Re: Interrupted exception chaining Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 16:26 +0200

csiph-web