Friday, January 11, 2013

difference between “Throw” and “Throw ex” in c#?


Throw ex

In Throw ex, the original stack trace information will get override and you will lose the original exception stack trace. I.e. 'throw ex' resets the stack trace.

Throw

In Throw, the original exception stack trace will be retained. To keep the original stack trace information, the correct syntex is 'throw' without specifying an exception.

Example

In the below example, Method2 and Method3 are used 'throw' keyword to re-throwing an exception and Method1 is used 'throw ex'. In the output of this example, you will see that Method2 and Method3 are used 'throw' keyword so stack trace is retained but Method1 used 'throw ex' so In main method stack trace reset and it lose the original stack trace from which the exception was generated.

class ThrowAndThrowEx
{
    static void Main()
    {
        try
        {
            Method1();
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n Handling exception in Main().");
            Console.WriteLine("Message :- \n" + ex.Message);
            Console.WriteLine("StackTrace :- \n" + ex.StackTrace);
        }

        Console.ReadLine();
    }

    static void Method1()
    {
        try
        {
            Method2();
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n Handling Exception in Method1()");
            Console.WriteLine("Message :- \n" + ex.Message);
            Console.WriteLine("StackTrace :- \n" + ex.StackTrace);
            throw ex;
        }
    }

    static void Method2()
    {
        try
        {
            Method3();
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n Handling Exception in Method2()");
            Console.WriteLine("Message :- \n" + ex.Message);
            Console.WriteLine("StackTrace :- \n" + ex.StackTrace);

            throw;
        }
    }

    static void Method3()
    {
        try
        {
            int i = 5;
            int j = 0;
            Console.WriteLine(i / j);
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n Handling Exception in Method3()");
            Console.WriteLine("Message :- \n"+ex.Message);
            Console.WriteLine("StackTrace :- \n" + ex.StackTrace);

            throw;
        }
    }
}

Output        

 Handling Exception in Method3()
Message :-
Attempted to divide by zero.
StackTrace :-
   at WindowsFormsApplication1.ThrowAndThrowEx.Method3() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 62


 Handling Exception in Method2()
Message :-
Attempted to divide by zero.
StackTrace :-
   at WindowsFormsApplication1.ThrowAndThrowEx.Method3() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 69
   at WindowsFormsApplication1.ThrowAndThrowEx.Method2() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 45


 Handling Exception in Method1()
Message :-
Attempted to divide by zero.
StackTrace :-
   at WindowsFormsApplication1.ThrowAndThrowEx.Method3() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 69
   at WindowsFormsApplication1.ThrowAndThrowEx.Method2() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 52
   at WindowsFormsApplication1.ThrowAndThrowEx.Method1() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 30


 Handling exception in Main().
Message :-
Attempted to divide by zero.
StackTrace :-
   at WindowsFormsApplication1.ThrowAndThrowEx.Method1() in G:\Example\MyTest\Wi
ndowsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 37
   at WindowsFormsApplication1.ThrowAndThrowEx.Main() in G:\Example\MyTest\Windo
wsFormsApplication1\WindowsFormsApplication1\ThrowAndThrowEx.cs:line 14
       

No comments:

Post a Comment