Tuesday, August 17, 2021

How throw is different from throw ex?

 

Throw and throw ex both are used to throw exception caught in catch block.

Throw: It preserve the stack trace.

             Example: Method1 throw Error1 and caught by Method2 and again this method throws Error2

             StackTrace: Method1 Error1 + Method2Error2



using System;

using System.Collections.Generic;

 namespace ConsoleApp2

{

     class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DevideNumberByZero(100);

            }

            catch (Exception ex)

            {

                throw;

            }

        }

         public static void DevideNumberByZero(int num)

        {

            try

            {

                Devide(num);

            }

            catch (Exception)

            {

                 throw;

            }

        }

        public static void Devide( int num)

        {

            try

            {

                int d = 0;

                int res = num / d;

                Console.WriteLine(res);

            }

            catch (Exception)

            {

                throw;

            }

        }

    }

}

 Here there are two methods Divide and DivideNumberByZero. Exception is thrown in Divide method and  it is caught by DivideNumberByZero, Exception is again throw by DevideNumberByZero method and it is caught by main method. You can see output in below. It is showing line number of all methods where exceptions were thrown.

Output:



Throw Ex: It does not preserve stack trace, so it will clear stack trace and will return only Method2Error2

              Example: Method1 throw Error1 and caught by Method2 and again this method throws Error2.


using System;

using System.Collections.Generic;

namespace ConsoleApp2

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DevideNumberByZero(100);

            }

            catch (Exception ex)

            {

                throw ex;

            }

                 

        }

        public static void DevideNumberByZero(int num)

        {

            try

            {

                Devide(num);

            }

            catch (Exception ex)

            {

                throw ex;

            }

          

        }

              public static void Devide( int num)

        {

            try

            {

                int d = 0;

                int res = num / d;

                Console.WriteLine(res);

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

   }

}

 Here there are two methods Divide and DivideNumberByZero. Exception is thrown in Divide method and  it is caught by DivideNumberByZero, Exception is again throw by DevideNumberByZero method and it is caught by main method. You can see output in below. It is showing line number of only line 16 in  main method where exceptions is caught not all line numbers as shown in above output.

Output:



No comments:

Post a Comment

Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...