Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Simple example for nested "new" expressions? Date: Sun, 17 Apr 2016 11:42:05 +0200 Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net 56fh1lg1tkWRTPGJW/6A9wD7Y0l9nAw7/6hLiYNPbWrjohE2I= Cancel-Lock: sha1:yWb7U93zH7BajDirZqZx3xkdDyg= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:34157 On 17.04.2016 06:11, Stefan Ram wrote: > ram@zedat.fu-berlin.de (Stefan Ram) writes: >> This is hard, because I already did an extensive search, but >> can you give any example of two nested new expressions that >> makes sense (is not bad style) and does neither require to >> explain exceptions or type arguments nor to use GUI classes >> nor to write class declarations? > > Ok, I found an example of wrapping an object with > another object that does make some sense and in > a sense meets the above requirements. > > It involves exceptions, which is ironic because above I said > that no knowledge of exceptions should be assumed. But it > does not use =C2=BBthrow=C2=AB, =C2=BBthrows=C2=AB, =C2=BBtry=C2=AB,= =C2=BBfinally=C2=AB or =C2=BBcatch=C2=AB > and uses the exceptions just =C2=BBas objects=C2=AB. > > public final class Main > { public static void main( final java.lang.String[] args ) > { > java.lang.Exception object > =3D new java.lang.RuntimeException > ( new java.lang.NoSuchMethodException() ); > > java.lang.System.out.println( object ); > java.lang.System.out.println( object.getCause() ); }} > > java.lang.RuntimeException: java.lang.NoSuchMethodException > java.lang.NoSuchMethodException > > This example is good, because > =C2=BBnew java.lang.RuntimeException > ( new java.lang.NoSuchMethodException() )=C2=AB > is a quotation from =C2=BBreal=C2=AB code, and the =C2=BBtoString()=C2= =AB > and the =C2=BBgetCause()=C2=AB methods help to explain the wrapping.= Only that in "real code" there is no point in creating two exceptions at = the same time. Rather, one is caught and a new one with a reference to=20 the caught one is created and thrown. I never saw the pattern you=20 present here in code other than for tests or experiments. Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/