Difference Between Throw and Throw ex in C#

Though many people don't pay much attention to these two common terms being used in exception handling, in interviews, interviewers frequently ask this difference. So let's see the difference between the two.

Let's write the code that is responsible for producing an exception and we will look at the stack trace produced during exception handling.

We will be implementing the code using "throw" first. Please pay attention to Method2 of Class2.

  1. namespace ExceptionHandlingSample  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Class1 cls1 = new Class1();  
  8.             Console.WriteLine(cls1.Method1());  
  9.             Console.Read();  
  10.         }  
  11.     }  
  12.   
  13.     class Class1  
  14.     {  
  15.         public String Method1()  
  16.         {  
  17.             try  
  18.             {  
  19.                 Class2 cls2 = new Class2();  
  20.                 return cls2.Method2();  
  21.             }  
  22.             catch (Exception ex)  
  23.             {  
  24.                 Console.WriteLine("-----Stack Trace for the Exception Occurred------");  
  25.                 Console.WriteLine(ex.StackTrace.ToString());  
  26.                 Console.WriteLine("-------Method in which Exception Occurred------");  
  27.                 Console.WriteLine(ex.TargetSite.ToString());  
  28.             }  
  29.             return "";  
  30.         }  
  31.     }  
  32.   
  33.     class Class2  
  34.     {  
  35.         public String Method2()  
  36.         {  
  37.             try  
  38.             {  
  39.                 FaultyMethod2();  
  40.                 return "Method2 called";  
  41.             }  
  42.             catch(Exception ex) {  
  43.                 throw;  
  44.             }  
  45.         }  
  46.         //This method is responsible for the exception  
  47.         public void FaultyMethod2()  
  48.         {  
  49.             throw new TimeoutException();  
  50.         }  
  51.     }   
  52. }  
So, in the preceding code we have a main method that calls the Method1 of Class1 using the object cls1.

In Method1, we call Method2 of Class2 using object cls2 that further calls FaultyMethod2 that produces the timeout exception.

The output of the preceding code is as follows:

Output

As we can see in the stack trace, we have a full hierarchy, in other words FaultyMethod2, then Method2 and then Method1 along with the line numbers at which an exception occurred or was thrown. Also, the target site is given as "FaultyMethod2".

Now let's change the "throw" to "throw ex" in Method2 and see the difference. For making this change, replace the catch block in Method2 with the following:
  1. catch(Exception ex) {  
  2.    throw ex;  
  3. }  
Now after running the program, the output will change to:

running the program

Now please notice that in the stack trace, we see hierarchy as Method2, then Method1 along with the line numbers at which an exception is thrown. Also, the target site is given as "Method2".

Thus, after looking at the output of the "throw" and "throw ex" we can say "throw" maintains the full hierarchy in the stack trace and gives complete information about the exception occurred in the code. Whereas "throw ex" pretends that exceptions occurred on the line where "throw ex" was written and removes all the hierarchy above the method containing the "throw ex" expression.

I hope this article helped you in understanding the difference between "throw" and "throw ex".

Happy coding.


Recommended Free Ebook
Similar Articles