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
{
DoSomethingDang erous();
}
catch (SillyException ex)
{
WriteExceptionT oLog();
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
ApplicationExce ption 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 internationaliz e 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 ApplicationExce ption, making the new class
serializeable, and then adding the additional fields & properties that I need.
"ahaupt@gmail.c om" wrote:
[color=blue]
> Hi all,
>
> Why would one want to rethrow an exception? Doesn't that defeat the
> purpose?
>
> Best,
> Andre
>
>[/color]
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.BaseValu e, Shared.Calculat eInterest(),
Shared.Generate PaymentSchedule ()
3. Shared may also then contain: Shared.Calculat eInterestExcept ion and
Shared.Generate PaymentSchedule Exception
4. The programmers who would use my library could put the
CalculateIntere s 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.
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.
--
Of all words of tongue and pen, the saddest are: "It might have been"
"Denny Boynton" wrote:
[color=blue]
> 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.
>
>[/color]
Thank you all for your contributions. Sure cleared it up for me.
Best,
Andre
billr wrote:[color=blue]
> 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.
>
>
> --
> Of all words of tongue and pen, the saddest are: "It might have been"
>
> Bill.Richards @ greyskin .co .uk
> http://greyskin.co.uk
>
>
> "Denny Boynton" wrote:
>[color=green]
> > 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.
> >
> >[/color][/color]
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))
--
Of all words of tongue and pen, the saddest are: "It might have been"
"ahaupt@gmail.c om" wrote:
[color=blue]
> Thank you all for your contributions. Sure cleared it up for me.
>
> Best,
> Andre
>
>
> billr wrote:[color=green]
> > 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.
> >
> >
> > --
> > Of all words of tongue and pen, the saddest are: "It might have been"
> >
> > Bill.Richards @ greyskin .co .uk
> > http://greyskin.co.uk
> >
> >
> > "Denny Boynton" wrote:
> >[color=darkred]
> > > 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.
> > >
> > >[/color][/color]
>
>[/color]
billr wrote:
[color=blue]
> 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.[/color]
Could you explain for me what on earth you are talking about? My news
reader doesn't have any feedback buttons.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
"Oliver Sturm" wrote:
[color=blue]
> billr wrote:
>[color=green]
> > 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.[/color]
>
> Could you explain for me what on earth you are talking about? My news
> reader doesn't have any feedback buttons.
>
>
> Oliver Sturm
> --
> omnibus ex nihilo ducendis sufficit unum
> Spaces inserted to prevent google email destruction:
> MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
> ICQ 27142619 http://www.sturmnet.org/blog
>[/color]
Comment