Difference Between Throw And Throw Ex In C#

We have seen and also read many blogs where people ask about the difference between Throw and Throw ex in C#. Generally we know about it but when these are asked in detail, we become confused. Here I am going to describe the steps about what happens in both cases.

We have written code here that is generating an exception so that we can watch the difference in both. First I am going write code for "Throw" case.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. namespace ConsoleApp2 {  
  6.     public class Program {  
  7.         public void Main(string[] args) {  
  8.             Employee emp = new Employee();  
  9.             Console.WriteLine(emp.GetDetails());  
  10.             Console.Read();  
  11.         }  
  12.     }  
  13.     class Employee {  
  14.         public String GetDetails() {  
  15.             try {  
  16.                 Department dept = new Department();  
  17.                 return dept.GetDepartments();  
  18.             } catch (Exception ex) {  
  19.                 Console.WriteLine("---Record Stack Trace for the Exceptions occurred----");  
  20.                 Console.WriteLine(ex.StackTrace.ToString());  
  21.                 Console.WriteLine("---Record method name in which Exception Occurred---");  
  22.                 Console.WriteLine(ex.TargetSite.ToString());  
  23.             }  
  24.             return "";  
  25.         }  
  26.     }  
  27.     class Department {  
  28.         public String GetDepartments() {  
  29.             try {  
  30.                 DivideByZero();  
  31.                 return "GetDepartments method called";  
  32.             } catch (Exception ex) {  
  33.                 throw;  
  34.             }  
  35.         }  
  36.         //We are generating exception in this method.  
  37.         public void DivideByZero() {  
  38.             throw new DivideByZeroException();  
  39.         }  
  40.     }  
  41. }  

We can see above that all the hierarchy of exceptions printed along with method names at which point exception occurred. Also the “TargetSite” has printed the name of method where exception has occurred.

Now let’s change the code for second case “Throw ex”.

For this we just change the throw with throw ex. Remaining code will be the same as above.

  1. catch (Exception ex)  
  2. {  
  3.     throw ex;  
  4. }  

Below is output in “throw ex” case.

As we can see in the above screen stack, only two methods “GetDepartments” and “GetDetails” are recorded. Method DivideByZero has not recorded in stack; this method is the cause of the exception. Since we have used “throw ex” in the method GetDepartments, the stack has recorded only “GetDepartments” and “GetDetails” and removed the DivideByZero from stack.

From both of the above codes we can say that “throw” maintains full hierarchy of exceptions in the stack trace and gives the complete exception information. Whereas “throw ex” returns the exception details till the point where the code “throw ex” is written and it removes rest exception details.

Thanks !!! I hope it will help.

Ebook Download
View all
Learn
View all