Exception Handling

P

Paul

suppose we just whant to blow up all exceptions that might occur to our
caller method, when we call some another method.We do not want to do
anything else (like finally stuff)
what's the difference between these code snippets:

<code>
SomeMethod();
</code>

and

<code>
try
{
SomeMethod();
}
catch (Exception ex)
{
throw ex;
}
</code>

Doesnt it really do the same ?

greets ;)
Paul
 
J

Jay B. Harlow [MVP - Outlook]

Paul,
Doesnt it really do the same ?
No, but almost.
throw ex;

Causes the stack trace on the exception to be reset to the current method,
in other words you loose the original stack trace.

If you simple do a "throw" instead of the "throw ex", they become the same.
try
{
SomeMethod();
}
catch (Exception ex)
{
throw;
}

From a previous question I answered on Try/Catch:

I am using allot the try catch in my code and the question is
if it is good ?
I would favor global exception handlers over a lot of Try/Catch statements,
however I have a number of Try/Finally statements to ensure resources are
disposed of properly. In other words: Try/Finally good, Try/Catch not so
good. See MSDN article below.
it will decrease my performance ?
A Try statement does not decrease performance, the act of throwing &
handling an exception will. In other words if you never throw an exception
there is no performance impact.
is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ?
If I had a problem in a Catch block, I would allow an exception to be raised
that would be handled by a higher catch block or one of my global exception
handlers.

</quote>
Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
 
C

Christopher Kimbell

These pieces of code do the same thing. If you have no try/catch/finally
blocks in your code, the execption will be routed up the call stack, when it
reaches the top, it will terminate the process, unless you have implemented
the UnhandledException event of the AppDomain.

I hope you have a try/catch/finnaly block in your calling code.

Chris
 

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