There are some posts that asks what the difference between those two are already.
(why do I have to even mention this...)
But my question is different in a way that I am calling "throw ex" in another error god-like handling method.
public class Program
{
public static void Main(string[] args)
{
try
{
// something
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void HandleException(Exception ex)
{
if (ex is ThreadAbortException)
{
// ignore then,
return;
}
if (ex is ArgumentOutOfRangeException)
{
// Log then,
throw ex;
}
if (ex is InvalidOperationException)
{
// Show message then,
throw ex;
}
// and so on.
}
}
If try & catch
were used in the Main
, then I would use throw;
to rethrow the error.
But in the above simplied code, all exceptions go through HandleException
Does throw ex;
has the same effect as calling throw
when called inside HandleException
?
-
Yes, there is a difference;
throw ex
resets the stack trace (so your errors would appear to originate fromHandleException
)throw
doesn't - the original offender would be preserved.
Scott Dorman : To expand on Marc's answer a bit, you can find more details here: http://geekswithblogs.net/sdorman/archive/2007/08/20/Difference-between-quotthrowquot-and-quotthrow-exquot-in-.NET.aspxSung Meister : @Scott: Thank you for the link. And I have also found out about how to extract error handler in the follow-up question: http://stackoverflow.com/questions/730300/how-to-throw-exception-without-resetting-stack-trace/730327#730327Shaul : This answer is incorrect! See http://stackoverflow.com/questions/730300/how-to-throw-exception-without-resetting-stack-trace/776734#776734Marc Gravell : @Shaul; no, it isn't. I've given details in a comment to your post.Shaul : @Marc Gravell - my apologies, you were right. Sorry about the downvote; it's too late for me to undo... :( -
No, this will cause the exception to have a different stack trace. Only using a
throw
without any exception object in thecatch
handler will leave the stack trace unchanged.You may want to return a boolean from HandleException whether the exception shall be rethrown or not.
-
when you do throw ex, that exception thrown becomes the "original" one. so all previous stack trace will not be there.
if you do throw, the exception just goes down the line and you'll get the full stack trace.
-
Also, In .NET 1.1 throw will also throw non .NET exceptions
-
(I posted earlier, and @Marc Gravell has corrected me)
Here's a demonstration of the difference:
static void Main(string[] args) { try { ThrowException1(); // line 19 } catch (Exception x) { Console.WriteLine("Exception 1:"); Console.WriteLine(x.StackTrace); } try { ThrowException2(); // line 25 } catch (Exception x) { Console.WriteLine("Exception 2:"); Console.WriteLine(x.StackTrace); } } private static void ThrowException1() { try { DivByZero(); // line 34 } catch { throw; // line 36 } } private static void ThrowException2() { try { DivByZero(); // line 41 } catch (Exception ex) { throw ex; // line 43 } } private static void DivByZero() { int x = 0; int y = 1 / x; // line 49 }
and here is the output:
Exception 1: at UnitTester.Program.DivByZero() in <snip>\Dev\UnitTester\Program.cs:line 49 at UnitTester.Program.ThrowException1() in <snip>\Dev\UnitTester\Program.cs:line 36 at UnitTester.Program.TestExceptions() in <snip>\Dev\UnitTester\Program.cs:line 19 Exception 2: at UnitTester.Program.ThrowException2() in <snip>\Dev\UnitTester\Program.cs:line 43 at UnitTester.Program.TestExceptions() in <snip>\Dev\UnitTester\Program.cs:line 25
You can see that in Exception 1, the stack trace goes back to the
DivByZero()
method, whereas in Exception 2 it does not.Take note, though, that the line number shown in
ThrowException1()
andThrowException2()
is the line number of thethrow
statement, not the line number of the call toDivByZero()
, which probably makes sense now that I think about it a bit... -
See also this question.
-
You can find more details here
0 comments:
Post a Comment