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


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

Re: Interrupted exception chaining

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Interrupted exception chaining
Date Tue, 25 Sep 2012 08:56:13 -0400
Organization A noiseless patient Spider
Lines 68
Message-ID <k3s9lk$8pl$1@dont-email.me> (permalink)
References <d203b18b-61ce-439e-9b1f-9cf056c1d161@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Tue, 25 Sep 2012 12:56:20 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="9013"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lUNE69fsAron092IRVh95"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
In-Reply-To <d203b18b-61ce-439e-9b1f-9cf056c1d161@googlegroups.com>
Cancel-Lock sha1:azIvmBnGIttNqePjoJIOxPBJxLw=
Xref csiph.com comp.lang.java.programmer:18911

Show key headers only | View raw


On 9/25/2012 6: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) {

     (Aside: This could probably be a `static' method.)

>      boolean done = false;
>      boolean interrupted = false;
>      synchronized (c) {
>          while (!done) {
>              try {
>                  c.wait();
>                  done = true;
>              } catch (InterrupedException ie) {

     (Aside: `InterrupttttttttttttttttttttedException'.)

>                  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?

     It seems you're trying to have things both ways: You "expect"
the interrupt by catching it, but then want to consider it as
"unexpected" anyhow.  Also, you want to inform your caller but
have chosen not to throw an informative exception.

     Since an exception is just an object, I suppose you *could*
have the method return it, or return `null' if there was none:

	/**
	 * Waits for notify() or notifyAll() on an object, in
	 * spite of interruptions.
	 * @param c The object to wait for.
	 * @return {@code null} if no interrupts occurred while
	 * waiting, or one of the {@code InterruptedException}s
	 * if one or more were thrown.
	 */
	public static InterruptedException
	        uninterruptableWait(Object c) {
	    InterruptedException ex = null;
	    for (;;) {
	        try {
	            c.wait();
	            return ex;
	        } catch (InterruptedException ie) {
	            ex = ie;
	        }
	    }
	}

     This looks vile to me, though.  I think your difficulty is
self-inflicted, and should be solved by rethinking your design.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

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