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


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

Re: Thumbs up for suppressable exceptions in JDK 1.7

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!.POSTED!ipv6.urchin.earth.li!twic
From Tom Anderson <twic@urchin.earth.li>
Newsgroups comp.lang.java.programmer
Subject Re: Thumbs up for suppressable exceptions in JDK 1.7
Date Tue, 27 Sep 2011 20:43:03 +0100
Organization Stack Usenet News Service
Lines 48
Message-ID <alpine.DEB.2.00.1109272034540.14737@urchin.earth.li> (permalink)
References <j5ru3o$cu7$1@news.albasani.net> <cp5387dadnj2jmuheklm82niu718054nsj@4ax.com>
NNTP-Posting-Host ipv6.urchin.earth.li
Mime-Version 1.0
Content-Type TEXT/PLAIN; charset=US-ASCII; format=flowed
X-Trace mud.stack.nl 1317152584 96287 2001:ba8:0:1b4::6 (27 Sep 2011 19:43:04 GMT)
X-Complaints-To abuse@stack.nl
NNTP-Posting-Date Tue, 27 Sep 2011 19:43:04 +0000 (UTC)
User-Agent Alpine 2.00 (DEB 1167 2008-08-23)
In-Reply-To <cp5387dadnj2jmuheklm82niu718054nsj@4ax.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8374

Show key headers only | View raw


On Tue, 27 Sep 2011, Roedy Green wrote:

> On Tue, 27 Sep 2011 09:31:03 +0200, Jan Burse <janburse@fastmail.fm>
> wrote, quoted or indirectly quoted someone who said :
>
>> Was just playing around with suppressable exceptions in JDK 1.7. This 
>> looks like a great improvement for bug hunting!
>>         ... 5 more
>
> I think you need some exposition on why this is a good thing.

It avoids this common mistake:

try {
 	doSomethingWhichMightThrowAnException();
}
finally {
 	doSomeCleanupWhichMightThrowAnException();
}

In that code, if both methods throw an exception, you will only see the 
second. The first exception - the one which actually caused the problem - 
will be lost. It's as if the VM has a very short attention span, and can 
only focus on whatever exception was most recently thrown.

In Java 7, you can put the cleanup into the close() method of an 
(Auto)Closeable, and use the try-with-resources form:

class Thing implements AutoCloseable {
 	public void close() throws AnException {
 		doSomeCleanupWhichMightThrowAnException();
 	}
}

try (Thing t = new Thing()) {
 	doSomethingWhichMightThrowAnException();
}

There, the compiler will arrange things so that if close() does throw an 
exception, it will be 'suppressed', and tagged on to the exception coming 
from doSomethingWhichMightThrowAnException() as a suppressed exception.

tom

-- 
We discovered in Nature's work a pattern of sloppiness, indifference to
basic scholarly standards, and flagrant errors so numerous they completely
invalidated the results. -- Encyclopaedia Britannica

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


Thread

Thumbs up for suppressable exceptions in JDK 1.7 Jan Burse <janburse@fastmail.fm> - 2011-09-27 09:31 +0200
  Re: Thumbs up for suppressable exceptions in JDK 1.7 Roedy Green <see_website@mindprod.com.invalid> - 2011-09-27 02:28 -0700
    Re: Thumbs up for suppressable exceptions in JDK 1.7 Jan Burse <janburse@fastmail.fm> - 2011-09-27 11:38 +0200
    Re: Thumbs up for suppressable exceptions in JDK 1.7 Tom Anderson <twic@urchin.earth.li> - 2011-09-27 20:43 +0100
      Re: Thumbs up for suppressable exceptions in JDK 1.7 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-29 18:05 +0200
        Re: Thumbs up for suppressable exceptions in JDK 1.7 Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-09-29 11:25 -0700
          Re: Thumbs up for suppressable exceptions in JDK 1.7 Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-29 23:57 +0200
          Re: Thumbs up for suppressable exceptions in JDK 1.7 Jan Burse <janburse@fastmail.fm> - 2011-09-30 11:30 +0200
        Re: Thumbs up for suppressable exceptions in JDK 1.7 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-09-29 18:57 -0300
  Re: Thumbs up for suppressable exceptions in JDK 1.7 Jan Burse <janburse@fastmail.fm> - 2011-10-02 13:38 +0200
    Re: Thumbs up for suppressable exceptions in JDK 1.7 Jan Burse <janburse@fastmail.fm> - 2011-10-02 13:45 +0200
      Re: Thumbs up for suppressable exceptions in JDK 1.7 Tom Anderson <twic@urchin.earth.li> - 2011-10-03 19:08 +0100

csiph-web