"Stefan Ram" <ram@[EMAIL PROTECTED]
> wrote in message
news:exceptions-20080509145219@[EMAIL PROTECTED]
> "Chronic Philharmonic" <karl.uppiano@[EMAIL PROTECTED]
> writes:
>>Returning null instead of the exception that was originally thrown
>>requires
>>the caller to check for null every time, or blindly use the null value
>
> I assume that one wants to copy a file and thus needs to open
> a file for reading and a file for writing and then invoke a
> copy operation.
>
> I now will write this once in the C style with zero checking
> and once in the Java style with a try statement.
>
> Does the try-style look more readable? Can it be simplified?
> (Possibly I was not using the simplest way to write it.)
>
> if( f = fopen( "alpha", "r" ))
> { if( g = fopen( "beta", "w" ))
> { if( copy( g, f ))re****t( "Copy failed." );
> close( g ); }
> else re****t( "Can't open beta for writing." );
> close( f ); }
> else re****t( "Can't open beta for writing." );
>
> try
> { f = fopen( "alpha", "r" );
> try
> { g = fopen( "beta", "w" );
> try
> { copy( g, f ); }
> catch( final Exception exception )
> { re****t( "Copy failed." ); }
> finally
> { close( g ); }}
> catch( final Exception exception )
> { re****t( "Can't open beta for writing." ); }
> finally
> { close( f ); }}
> catch( final Exception exception )
> { re****t( "Can't open alpha for reading." ); }
Without specifically rewriting your logic, I can only say that this isn't
how my exception handlers typically look. I tend to use one big try block,
with several catch blocks at the end, to deal with things that I can deal
with, or let the exception fly if I can't deal with it, or if I want the
exception itself to re****t the failure. I don't usually concern myself
with
re****ting things like "copy failed" at the granular level.
It is difficult for me to provide examples of what I mean that fit in a
single method or code snippet, because structured exception handling is
more
of an architectural endeavor, requiring a more holistic approach to error
handling. I could send you the source code to my WxService project
(http://mysite.verizon.net/Karl_Uppiano/wxservice.html)
if you're
interested.


|