Click here to Skip to main content
15,885,767 members
Articles / All Topics

Handle Exception Carefully

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
25 Jul 2011CPOL 13K   6   1
How to handle exception carefully

Handle Exception carefully means I am not going to discuss some rocket science about exception handling but I am going to discuss how not to shadow the exception in your program. I am not going to discuss more. I am starting my example.

C#
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Program p = new Program();
                p.MethodA();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }

        public void MethodA()
        {
            try
            {
                MethodB();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MethodB()
        {
            try
            {
                MethodC();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MethodC()
        {
            int a = 10;
            int b = 0;
            int c = 0;
            try
            {
                c = a / b;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

When executing the program, you will get the following output as result. Yes, you will be able to handle the exception thrown by the method and displayed on the console.

Output

Image 1

But the actual thing is you are hiding the stack trace and each time you throw the exception by writing throw ex, you are creating a new stack trace. So you are not able to get the actual path from where the exception gets thrown.

Now to understand the thing properly, write the program again and now replace throw ex by the throw:

C#
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Program p = new Program();
                p.MethodA();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }

        public void MethodA()
        {
            try
            {
                MethodB();
            }
            catch
            {
                throw;
            }
        }

        public void MethodB()
        {
            try
            {
                MethodC();
            }
            catch
            {
                throw;
            }
        }

        public void MethodC()
        {
            int a = 10;
            int b = 0;
            int c = 0;
            try
            {
                c = a / b;
            }
            catch
            {
                throw;
            }
        }
    }

When executing the program, you will get the following output:

Output

Image 2

Now as you see when you replace the line, there is no new stacktrace that gets created and you get the actual path from where exception gets thrown.

Summary

So make use of throw to get the actual path of the exception rather than using throw ex.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 4 Pin
mandar130525-Jul-11 20:06
mandar130525-Jul-11 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.