views:

1934

answers:

7

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?

+30  A: 

Yes, there is a difference;

  • throw ex resets the stack trace (so your errors would appear to originate from HandleException)
  • throw doesn't - the original offender would be preserved.
Marc Gravell
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.aspx
Scott Dorman
@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#730327
Sung Meister
This answer is incorrect! See http://stackoverflow.com/questions/730300/how-to-throw-exception-without-resetting-stack-trace/776734#776734
Shaul
@Shaul; no, it isn't. I've given details in a comment to your post.
Marc Gravell
@Marc Gravell - my apologies, you were right. Sorry about the downvote; it's too late for me to undo... :(
Shaul
+2  A: 

No, this will cause the exception to have a different stack trace. Only using a throw without any exception object in the catch handler will leave the stack trace unchanged.

You may want to return a boolean from HandleException whether the exception shall be rethrown or not.

Lucero
+1  A: 

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.

silverCORE
+2  A: 

Also, In .NET 1.1 throw will also throw non .NET exceptions

A: 

(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() and ThrowException2() is the line number of the throw statement, not the line number of the call to DivByZero(), which probably makes sense now that I think about it a bit...

Shaul
+1  A: 

See also this question.

Joshua Fox
A: 

You can find more details here

Difference between "throw" and "throw ex"

shailesh