Difference between Throw and Throw ex in .Net

First this post will be in English, I need to practice again  :-).

I recently learnt that statements Throw and Throw ex in the  .Net framework’s exception handling process don’t beget the same behavior.

Here’s our test example of exception handling :

 public class ClassTest
    {
        public ClassTest()
        {
            try
            {
                this.MethodExceptionCaught();
            }
            catch (Exception ex)
            {

            }
        }

        public void MethodExceptionCaught()
        {
            try
            {
                this.MethodExceptionOriginated();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void MethodExceptionOriginated()
        {
            throw new Exception("Problem occured");
        }
   }  

I have three methods :

  • MethodExceptionOriginated : The method where the exception is first raised
  • MethodExceptionCaught : The method calling the preceding method and implementing a try catch block.
  • ClassTest : The constructor and the launcher method for the test

So let’s debug it by putting a breakpoint in the catch block of the constructor. Here’s the result of the exception as shown in the quick watch for the StackTrace property :

à Certif1.ClassTest.MethodExceptionOriginated() dans c:\Workspace\Dotnet\Certifications\Certif1\Certif1\BlogExceptionHandling.cs:ligne 37
à Certif1.ClassTest.MethodExceptionCaught() dans c:\Workspace\Dotnet\Certifications\Certif1\Certif1\BlogExceptionHandling.cs:ligne 31
à Certif1.ClassTest..ctor() dans c:\Workspace\Dotnet\Certifications\Certif1\Certif1\BlogExceptionHandling.cs:ligne 15

Ok so we got all the stack trace information, from the method it originated to the method it is last caught. But if we consider a production environment or an API you published to other coders, you may want to manage exceptions but not to reveal your product intern architecture. You can do this by using Throw ex statement instead of just Throw. Look at the new code, I just add Throw ex to the MethodExceptionCaught code :

        public void MethodExceptionCaught()
        {
            try
            {
                this.MethodExceptionOriginated();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Let’s run a debug again and here’s the information in the StackTrace property of my exception :

à Certif1.ClassTest.MethodExceptionCaught() dans c:\Workspace\Dotnet\Certifications\Certif1\Certif1\BlogExceptionHandling.cs:ligne 31
à Certif1.ClassTest..ctor() dans c:\Workspace\Dotnet\Certifications\Certif1\Certif1\BlogExceptionHandling.cs:ligne 15

All the other information are the same except the StackTrace property which has been reset. We cannot know from which method the exception originated.

I know that seems it’s a subtle difference but actually that can be useful to hide the intern mechanism of your code. On the other side that also can make your debugging trickier because you won’t know where your exception has been raised first.

Leave a Reply