Monday, October 21, 2013

throw new exception- C#

This post will be in response to my older post about throw exception best practice. where one of user asked that I should include throw new exception.So I thought it will be good idea to write a whole blog post for it. This blog post explains what's wrong with throw new exception.

What’s wrong with throw new exception:

Throw new exception is even worse, It will create a new exception and will erase all the earlier exception data. So it will erase stack trace also.Please go through following code. It’s same earlier post the only difference is throw new exception.

using System;

namespace Oops
    class Program
        static void Main(string[] args)
            catch (Exception exception)
                throw new Exception
                    "Brand new Exception-Old Message:{0}",

        public  static void DevideByZero(int i)
           int j = 0;
           int k = i/j;

Now once you run this example. You will get following output as expected.

Throw new exception C#

Hope you like it. Stay tuned for more..


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews