473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why rethrow an exception?

Hi all,

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

Best,
Andre

Nov 17 '05 #1
10 6901
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.
}

Nov 17 '05 #2
-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.

"ah****@gmail.com" wrote:
Hi all,

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

Best,
Andre

Nov 17 '05 #3
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.

Nov 17 '05 #4
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.

Nov 17 '05 #5
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:
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.

Nov 17 '05 #6
Thank you all for your contributions. Sure cleared it up for me.

Best,
Andre
billr wrote:
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:
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.


Nov 17 '05 #7
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"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"ah****@gmail.com" wrote:
Thank you all for your contributions. Sure cleared it up for me.

Best,
Andre
billr wrote:
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:
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.


Nov 17 '05 #8
billr wrote:
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
--
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
Nov 17 '05 #9
http://msdn.microsoft.com/newsgroups...4-235ecc03bd45

there are feedback buttons here

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Oliver Sturm" wrote:
billr wrote:
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
--
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

Nov 17 '05 #10
billr <bi***@discussions.microsoft.com> wrote:
http://msdn.microsoft.com/newsgroups...crosoft.public
.dotnet.languages.csharp&mid=9f17c986-6523-4db2-9aa4-235ecc03bd45

there are feedback buttons here


Please note that not everyone - possibly not even the majority of
people - use the web based newsreader. Many of us use dedicated NNTP
clients.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gil | last post by:
In C++, I can rethrow the exception I just caught with the throw statement. Can I do something similar in Java? } catch (Exception ex) { throw; }
5
by: A | last post by:
Hi, I'm having some difficulty understanding the semantics of the rethrow keyword. Consider the following code: int main(){ try{ ... } catch(SomeException &SE){
8
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
6
by: cmay | last post by:
I'm just looking for some other opinions on this. I have a control that does employee lookups, auto complete of the name etc. Let say that this control is:...
24
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
1
by: Jason S | last post by:
I haven't used try/catch/finally very much in Javascript. My function (let's call it try_it()) needs to call a function that could throw an exception (let's call it dangerous()) with some setup()...
8
by: reuce-google | last post by:
Hi folks, I want to store an exception and rethrow it later: CException m_pEc = NULL; // Class variable. try { throw new CMemoryException(); }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.