Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Handle Exception Carefully

, 25 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Handle Exception carefully means I am not going to discuss some rocket science about exception handling but I am going to discuss not to shadow the exception in your program. Not going to discuss more I am starting my exampleclass Program { static void Main(string[] args) {
Handle Exception carefully means I am not going to discuss some rocket science about exception handling but I am going to discuss not to shadow the exception in your program. Not going to discuss more I am starting my example
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 execute the program you will get the following output as result. Yes you can able to handle the exception thrown by the method and displayed on the console.
Output
But the actual thing is you hiding the stack trace and each time you throw the exception by writing throw ex you are creating new stack trace. So you are not able to get the actual path from where the exception get thrown.

Now to understand the thing properly, write the program again and now replace throw ex by the throw
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 execute the program get the following output
Output
Now as you see when you replace the line there is no new stacktrace get created and you get actual path from where exception get thrown.
Summary
So make use of the 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)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in 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:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 Pinmembermandar130525-Jul-11 21:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid