The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Exceptions



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Raghu Havaldar on May 05, 1999 at 11:04 AM

> Bill Venners wrote:

> > My own thought is that one should catch exceptions in the
> > method that knows how to handle that exception, and that is
> > usually going to be at the application level. Exceptions let
> > you transfer control from the part of a program where an
> > error or problem is detected to another part of the program
> > that knows how to handle the error.

> This is good as far as it goes but I wonder about how locally
> you should catch exceptions. Suppose you have a method that
> makes several calls to an API, and each call can throw a couple
> of different exceptions. Do you put a little try-catch block
> around each call, or a big one around the whole method? Any
> style guidelines? Thanks.

I think the approach here would be context-dependent.

Lets say you have method calls, m1, m2, m3 and m4 - with each
throwing 'checked' exceptions. If m1 and m2 need to be executed together to fulfill a part of the functionality of the method, then wrap both of them in one try/catch block. If they are independent - (one can proceed without the other)in terms of the context, then place them in a separate try/catch block.

Another approach: If either m1 or m2 resulted in an exception with which the method cannot continue, return from the method. This indicates how the try/catch blocks need to be placed. In many scenarios, the method can proceed after catching an exception, which influences the try/catch blocks.

any other suggestions...


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us