Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EDAD5102C3 for ; Sun, 27 Oct 2013 05:52:19 +0000 (UTC) Received: (qmail 88613 invoked by uid 500); 27 Oct 2013 05:52:09 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 88510 invoked by uid 500); 27 Oct 2013 05:51:57 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 88493 invoked by uid 99); 27 Oct 2013 05:51:50 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Oct 2013 05:51:50 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of damjan.jov@gmail.com designates 209.85.212.171 as permitted sender) Received: from [209.85.212.171] (HELO mail-wi0-f171.google.com) (209.85.212.171) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Oct 2013 05:51:44 +0000 Received: by mail-wi0-f171.google.com with SMTP id h11so2584782wiv.16 for ; Sat, 26 Oct 2013 22:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=u/BRUXbsj1VhI0LppJ1UDVlvhm89ZXlcjLBS162W6fs=; b=S+B4/ez/4+wdTZgd3t5/ywfM/jvF7HT9lIr5efgkYj2/sZ6Az+yUfMPvg9TPLL/bUd GNaaXH3zl1x6hCndACZUDMrjF5afONK/LRbhlpKqsmxay2nG91CkCSby+AGMbJRp0jgY 2DhuPugviWXJYCSaq5dxmrNP7FR/dyzH+K5PbhIu96pSSvbXPX7xXCrp80L+Mm0FhZi8 LXY1BJwzilOOhCRTFRh7MdqxNy5VND1l9rWr5wVJB8GZuCub+NSRd/AuCYs25CkMNnZF bMOqRnzp+X4kDbsuwAUHWnVcdyPtx4QXTsjCGAOGQqjxLldKpmBj+TG1LA/+RWmghbU1 GZBw== X-Received: by 10.180.185.101 with SMTP id fb5mr4331767wic.11.1382853084207; Sat, 26 Oct 2013 22:51:24 -0700 (PDT) MIME-Version: 1.0 Received: by 10.216.124.6 with HTTP; Sat, 26 Oct 2013 22:51:03 -0700 (PDT) In-Reply-To: References: From: Damjan Jovanovic Date: Sun, 27 Oct 2013 07:51:03 +0200 Message-ID: Subject: Re: [imaging] Closing stream To: Commons Developers List , joerg.schaible@gmx.de Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org an On Fri, Oct 25, 2013 at 5:24 PM, J=F6rg Schaible wr= ote: > Hi Damjan, > > Damjan Jovanovic wrote: > >> On Fri, Oct 25, 2013 at 12:36 PM, J=F6rg Schaible >> wrote: >>> Hi Damjan, >>> >>> Damjan Jovanovic wrote: >>> >>> [snip] >>> >>> Thanks for explanation. >>> >>>> We would be able to adapt that for Java < 1.7 by swallowing the close >>>> exception instead of calling addSuppressed() on the primary exception, >>>> but the show stopper is catching and rethrowing the primary exception >>>> (Throwable), which javac < 1.7 refuses to compile because it doesn't >>>> do "Rethrowing Exceptions with More Inclusive Type Checking" >>>> (http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-= multiple.html). >>>> >>>> But this would work and always sets succeeded correctly without >>>> catch/re-throw: >>>> >>>> final InputStream is =3D factoryMethodThatCanThrow(); >>>> boolean succeeded =3D false; >>>> try { >>>> try { >>>> is.methodThatCanThrow(); >>>> } finally { >>>> } >>>> succeeded =3D true; >>>> } finally { >>>> closeSafely(!succeeded, is); >>>> } >>> >>> I guess the nested try was unintentionally ;-) >>> >>> Cheers, >>> J=F6rg >> >> Well that actually won't work, because the "succeeded =3D true;" will be >> skipped if there is a "return;" in the inner try. > > Well, but this has to be done in our code, so we can either change it or = set > "succedded =3D true" before the return as well. > > To mimic Java 7, we could also implement: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D %< =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > Throwable t =3D null; > try { > } (catch IOException e) { t =3D e; throw e; } > // ... another line for each checked exception > } (catch RuntimeException e) { t =3D e; throw e; } > } (catch Error e) { t =3D e; throw e; } > } finally { > closeSafely(t !=3D null, is); > } > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D %< =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > > but as commented, we have to add a catch for every checked exception, > anotehr one for RuntimeException and one for Error. The approach with the > succeeded flag seems easier ... > >> Other than a custom Java compiler, I guess there's no clean way of >> doing this in Java < 1.7. There's really only option 2 - with being >> careful to always set succeeded correctly on all paths out of the try >> block. Almost like releasing memory in C. > > Yep. > > Cheers, > J=F6rg > One thing that amuses me to no end, is that while it's at least a solved problem in Java 7, exceptions thrown from C#'s Dispose() method in a using() block always swallow the original exception, just like an uncaught close() exception in Java's finally :) (http://blogs.infosupport.com/the-c-using-statement-and-suppressed-exceptio= ns-compared-to-java-7-try-with/). Anyway I now think the way forward is Java 7. Java 5 was EOL since 3 November 2009, and Java 6 was EOL since February 2013 (with a last update on 6 March 2013). There are ways of getting Java 7 features like try-with-resources on Android (https://github.com/yareally/Java7-on-Android) and besides Imaging's use of java.awt.* packages is a bigger barrier there. Applications and JVMs will eventually support Java 7 anyway, and even if they don't, a special compiler could produce binaries for earlier versions of Java. Can I just go change the POM to Java 7 or do we need a vote? Damjan --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org