Why rethrow an exception?

  • Thread starter Thread starter ahaupt
  • Start date Start date
A

ahaupt

Hi all,

Why would one want to rethrow an exception? Doesn't that defeat the
purpose?

Best,
Andre
 
Sometimes you might want to do some tracing logic, or whatever, before
allowing the exception to be handled by the next tier. Maybe my data
layer wants to write to a log, then rethrow to the business logic layer
which catches it and tries the operation again, or simply allows it to
propagate to the UI layer which displays an error.

try
{
DoSomethingDangerous();
}
catch (SillyException ex)
{
WriteExceptionToLog();
throw; // This will let my UI handle the error as it sees fit.
}
 
-Tracing, sometimes it's good to mark the actual place in code where the
exception came from. By re-throwing the exception the an additional line of
diagnostic information is added to the exception's stack trace...

- To make a more "user friendly" error message. If a raw SQL Server
exception bubbled up tot he GUI the user would probably not understand it.
To make a more friendly message I would create a new exception class, with
ApplicationException as a base, and then pass my "friendly" message and the
raw exception to the base class constructor... You could also do this if you
need to internationalize your friendly message; you can pass a string
resource as your message rather than the, probably english, original error
message...

- Also, suppose I want to add additional information to my exception such as
the actual text of a SQL query, or perhaps the user identity, or maybe the
datetime of the exception. I can do this by making a new exception class,
again descending from ApplicationException, making the new class
serializeable, and then adding the additional fields & properties that I need.
 
We use catch-throw for several things, namely (as mentioned) obtaining
more information for troubleshooting on our end (as developers). We
capture an error, and throw it up further, adding information as
needed, then allowing our top level ErrorHandler to catch it and write
out the exception information to a logfile, all while displaying a
simple message to the user.

Ifthe EmailOnError bit is set, programming receives a notification of
the errors occurrance, time, date, briefe description. At that point we
can check the error.log file for what exactly happened.

Simply put, it makes it easier for both the user and the developer to
perform things certain ways.

Another reason would be if you were writing your own basic libraries.
If you add an exception type to them and only throw those types the
higher up programmers will be able to handle your exceptions with
grace.

For instance:
1. I develop library Shared.dll
2. Shared contains: Shared.BaseValue, Shared.CalculateInterest(),
Shared.GeneratePaymentSchedule()
3. Shared may also then contain: Shared.CalculateInterestException and
Shared.GeneratePaymentScheduleException
4. The programmers who would use my library could put the
CalculateInteres method call into a try/catch block and check for that
specific exception.
 
I agree with everything in the thread and just wanted to add an
additional note. I see a lot of this in code reviews where exceptions
are being rethrown:

catch(Exception ex)
{
...some logging or other activity
throw ex;
}

As the second post on this thread shows, this should always be just
throw;

throw ex; obfuscates the original context of the exception.

I just thought I'd throw that $.02 in.
 
Another pooint to make (here's my $.02 worth) is that just because you caught
an Exception does not necessarily mean that your application is in a stable
state to continue. In this case, you will want the Exception to propogate all
they way up to your UI where you will ultimately handle it perhaps by doing
your best to terminate gracefully.
 
Perhaps you would be kind enough to open the responses you found helpful and
click on the button to indicate that the post was helpful to you.

Other users in the community will benefit from this action, since they will
be able to see what other users have deemed helpful responses when they
search for previous posts pertaining to their current problem (that is
assuming that the op will search instead of just posting without doing any
research for themselves)

Cheers Bill :o))
 
billr said:
Perhaps you would be kind enough to open the responses you found helpful and
click on the button to indicate that the post was helpful to you.

Could you explain for me what on earth you are talking about? My news
reader doesn't have any feedback buttons.


Oliver Sturm
 

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

Back
Top