Difference between throw and throw ex in C#

This question is mostly asked in the interview. So guys be prepared for this question. And Let us understand it through an example.

Exception Bubbling

throw and throw ex both are used to throw an Exception. We can also re-throw an Exception. Throw passes the exception to its caller. The caller can then have the chance to do something with that exception. This is known as Exception Bubbling in C#. throw and throw ex both can re-throw the exception But there is difference when we look at their stack Trace. (Tracing the path of the exception from the method which causes the exception).




Don’t worry if you have some doubt left, Go through the example given below and surely you will understand the concept of both the terms.

class Client
    {
        static void Main(string[] args)
        {
            try
            {
                Customer1 cus1 = new Customer1();
                Console.WriteLine(cus1.Method1());
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine("*******Exception Flow - Stack Trace*******");
                Console.WriteLine(ex.StackTrace.ToString());
                Console.WriteLine(ex.Message.ToString());
                Console.WriteLine("*******Source Where Exception Occurred*******");
                Console.WriteLine(ex.TargetSite.ToString());
                Console.Read();
            }
        }  
    }
 class Customer1
    {
        public String Method1()
        {
            try
            {
                Customer2 cus2 = new Customer2();
                return cus2.Method2();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "Method1 Called";
        }
    }
 class Customer2
    {
        public String Method2()
        {
            try
            {
                FaultyMethod();
                return "Method2 called";
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        <span class="font-green">//This method is responsible for the exception  </span>
        public void FaultyMethod()
        {
            throw new TimeoutException();
        }
    }   

Here in the above example we have 3 classes – client class where our main method resides, Customer1 class having one method ‘Method1‘, Customer2 class having 2 methods – ‘Method2‘ and ‘FaultyMethod‘.

Understanding the path from where Exception happens to where it is going :
FaultyMethod() -> Method2() -> Method1() -> Main()

Output :throw

Now do one thing in the catch block of ‘Method1’ replace throw with throw ex.
Throwex

Now carefully observe the Output:
throwex

TIP :
– ex is the object of Exception Class
– you can use throw in naked Catch block but not throw ex.

try { // something goes here }
catch {
throw
}

– ex.TargetSite => returns the method that throws the exception
– ex.Source => returns the name of application that causes the exception