Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe20.iad.POSTED!83aa503d!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Thumbs up for suppressable exceptions in JDK 1.7 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 74 Message-ID: X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Thu, 29 Sep 2011 18:25:59 UTC Date: Thu, 29 Sep 2011 11:25:57 -0700 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8401 On 9/29/11 9:05 AM, Daniele Futtorovic wrote: > On 27/09/2011 21:43, Tom Anderson allegedly wrote: >> On Tue, 27 Sep 2011, Roedy Green wrote: >> >>> On Tue, 27 Sep 2011 09:31:03 +0200, Jan Burse >>> 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 >> > > Indeed, but even more generally, we can from now on register > "suppressed" exceptions ourselves, as Jan's code and the JSE7 Javadoc > for java.lang.Throwable show. Great Thing IMHO. Closes a hole that's > been lurking there for a very long time. > > Thanks Jan for bringing this to my attention. > This will definitely be a big help debugging issues. It may also help fill up log files faster ;-). I can see it being extremely useful in a try/catch/retry scenario (such as a service which tries a few times to connect to a remote machine, and gets a different error each time). I want to read up on it, but I wonder if it will help with this (unfortunate) situation: try { buggyCodeThrowsNullPointerException(); } catch (Exception oopsThisWasIgnoredByLazyProgrammer) { throw new BusinessLayerSpecificException( "Unable to process request for no good reason."); } This suppresses a bug, but the original programmer wasn't thinking about potential bugs, only recoverable exceptions. The exception is effectively suppressed.