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


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

Re: Simple example for nested "new" expressions?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Simple example for nested "new" expressions?
Date 2016-04-17 11:42 +0200
Message-ID <dnh43eFagoaU1@mid.individual.net> (permalink)
References <nested-new-20160416024157@ram.dialup.fu-berlin.de> <nested-news-20160417050851@ram.dialup.fu-berlin.de>

Show all headers | View raw


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 »throw«, »throws«, »try«, »finally« or »catch«
>    and uses the exceptions just »as objects«.
>
> public final class Main
> { public static void main( final java.lang.String[] args )
>    {
>      java.lang.Exception object
>      = 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
>     »new java.lang.RuntimeException
>    ( new java.lang.NoSuchMethodException() )«
>    is a quotation from »real« code, and the »toString()«
>    and the »getCause()« 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 
the caught one is created and thrown.  I never saw the pattern you 
present here in code other than for tests or experiments.

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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


Thread

Re: Simple example for nested "new" expressions? Robert Klemme <shortcutter@googlemail.com> - 2016-04-17 11:42 +0200
  Re: Simple example for nested "new" expressions? Martin Gregorie <martin@address-in-sig.invalid> - 2016-04-17 11:52 +0000

csiph-web