Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18911
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Interrupted exception chaining |
| Date | 2012-09-25 08:56 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <k3s9lk$8pl$1@dont-email.me> (permalink) |
| References | <d203b18b-61ce-439e-9b1f-9cf056c1d161@googlegroups.com> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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