Sunday 17 January 2016

Difference between the throw and throw ex in C#

In this post we are going to see what is the major difference between the throw and throw ex in C#, Normally this statement is uses inside the catch statement, to throw the exception to  parent method or scope.

Let we see what is the major difference , when we try to throw a exception, it will return the error to the parent, but when u try to throw using throw ex, then the stack trace of that exception is start from that point, i.e is previous point stace trace are not present there, Let we see with some example.

public class Employee
    {
        public void AddEmployee(int id,string name)
        {
            Department dep = Department.GetDepartment(0);            
        }
}

    public class Department
    {
        public string Name { setget; }

        public static Department GetDepartment(int id)
        {
            int e = 1 / id;

            if (e == 0)
                return new Department() { Name = "CSE" };
            else
                return new Department() { Name = "EEE" };


        }
    }

 static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Exception with throw ex");
                Console.WriteLine("*****************************");
                TestError();
                
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine("\n\n");
            }


            try
            {
                Console.WriteLine("Exception with throw");
                Console.WriteLine("*****************************");
                TestError1();
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }


            Console.Read();
        }

        private static void TestError()
        {
            try
            {
                Employee emp = new Employee();
                emp.AddEmployee(1, "ee0");
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
                throw ex;
            }
        }

        private static void TestError1()
        {
            try
            {
                Employee emp = new Employee();
                emp.AddEmployee(1, "ee0");
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
                throw ;
            }
        }




From the above code you can see the output that the TestError method throw the error like throw ex, when we see the stack trace the exception starts from the method TestError() then to the Main Method, instead of showing the actual error place the trace are start from the TestError method, but in TestError1 we can see the full stack trace where the actual error occur.

so the difference is the Stack Trace of the Error is actually not returned when we throw like throw ex;


From this post you can understand the difference between throw and throw ex in C#

No comments:

Post a Comment