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


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

Re: Simple example for nested "new" expressions?

Started byRobert Klemme <shortcutter@googlemail.com>
First post2016-04-17 11:42 +0200
Last post2016-04-17 11:52 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#34157 — Re: Simple example for nested "new" expressions?

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-04-17 11:42 +0200
SubjectRe: Simple example for nested "new" expressions?
Message-ID<dnh43eFagoaU1@mid.individual.net>
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/

[toc] | [next] | [standalone]


#34158

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-04-17 11:52 +0000
Message-ID<nevtds$6a0$1@dont-email.me>
In reply to#34157
On Sun, 17 Apr 2016 11:42:05 +0200, Robert Klemme wrote:

> 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.
> 
Agreed - and when I do that its usually because I want a single exception 
to be thrown by a class I'm writing, so MyClass only throws 
MyClassException. These exceptions are either constructed to indicate 
class-specific problems or to report other exceptions I want to deal with 
in the same way, e.g. inside the class:

  try { ... code which may throw MyClassException on finding a
            problem... }
  catch (Exception e) { throw new MyClassException(e.getMessage()) }
  ...

An example would be a class that validates a configuration file: most of 
the exceptions it can throw will report invalid configuration settings or 
syntax but it is sensible to treat 'configuration file not found' in the 
same way because its also a configuration error as far as the larger 
process is concerned: either a missing configuration file or an invalid 
configuration should stop the process after listing configuration-related 
problems.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web