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


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

Re: Simple example for nested "new" expressions?

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Simple example for nested "new" expressions?
Date 2016-04-17 11:52 +0000
Organization A noiseless patient Spider
Message-ID <nevtds$6a0$1@dont-email.me> (permalink)
References <nested-new-20160416024157@ram.dialup.fu-berlin.de> <nested-news-20160417050851@ram.dialup.fu-berlin.de> <dnh43eFagoaU1@mid.individual.net>

Show all headers | View raw


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       |

Back to comp.lang.java.programmer | Previous | Next — Previous 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