Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34157
| Path | csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| 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 | <dnh43eFagoaU1@mid.individual.net> (permalink) |
| References | <nested-new-20160416024157@ram.dialup.fu-berlin.de> <nested-news-20160417050851@ram.dialup.fu-berlin.de> |
| 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 | <nested-news-20160417050851@ram.dialup.fu-berlin.de> |
| Xref | csiph.com comp.lang.java.programmer:34157 |
Show key headers only | 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
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