Exception

A

Anders Eriksson

I have been thought that you shouldn't use the generic Exception.
Instead you should use the specific exception that the functions you are
calling in the try clause are throwing.

But what if the function has a number of exceptions and I want to do the
same thing for all the exceptions, e.g. log the error to a log file
and/or show the error on screen.

Would it be acceptable to use catch(Exception e)?

or is it still better to use all of the specific exceptions

// Anders
 
R

Rick Lones

Anders said:
I have been thought that you shouldn't use the generic Exception.
Instead you should use the specific exception that the functions you are
calling in the try clause are throwing.

But what if the function has a number of exceptions and I want to do the
same thing for all the exceptions, e.g. log the error to a log file
and/or show the error on screen.

You may not even know what all the possible exceptions are in the case where
functions call other functions, etc., unless all of the code is yours.
Would it be acceptable to use catch(Exception e)?

Perfectly fine.
or is it still better to use all of the specific exceptions

That would be silly in the situation you describe. Case after case, all the
same . . . your instinct is correct. Catching unexpected errors generically for
logging is a pretty common requirement, in my experience.

Mixed strategies are also often appropriate - for example, when certain
"expected" exceptions may get handled in special ways (e.g., retries after a
timeout), but you also want to catch and log all other "unexpected" exceptions.

-rick-
 
R

Registered User

I have been thought that you shouldn't use the generic Exception.
Instead you should use the specific exception that the functions you are
calling in the try clause are throwing.

But what if the function has a number of exceptions and I want to do the
same thing for all the exceptions, e.g. log the error to a log file
and/or show the error on screen.

Would it be acceptable to use catch(Exception e)?

or is it still better to use all of the specific exceptions
If the identical action is going to be taken regardless of the
exception type, the actual type of exception is unimportant; use
catch(Exception ex) or maybe just catch.

If the action to be taken is specific to the type of exception, the
actual type of exception is important; use specific catch blocks.

Sometimes a combination of two is the way to go.

try
{
...
}
catch(FileNotFoundException ex)
{
// action specific to FileNotFoundException
...
}
catch(Exception ex)
{
// action common to all non-specified exception types
...
}

regards
A.G.
 
A

Arne Vajhøj

I have been thought that you shouldn't use the generic Exception.
Instead you should use the specific exception that the functions you are
calling in the try clause are throwing.

But what if the function has a number of exceptions and I want to do the
same thing for all the exceptions, e.g. log the error to a log file
and/or show the error on screen.

Would it be acceptable to use catch(Exception e)?

or is it still better to use all of the specific exceptions

If you intend to exit the program (after logging/displaying)
then it is OK to catch Exception.

Otherwise it is not.

You will not know for sure that you can recover after
all types of exceptions.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top