65.9K
CodeProject is changing. Read more.
Home

Exceptions Handling - Some Advanced Detail

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.63/5 (9 votes)

Jan 31, 2010

CPOL

2 min read

viewsIcon

18446

What is the difference between 'throw e' and just 'throw'

Introduction

This article elucidates the difference between throw; and throw e;.

Background

I assume you work with try/catch/finally blocks in C# and throw operator.

Meat

The difference is in tracing information that the end catcher has got. Namely it causes information that is stored in StackTrace and TargetSite. By the way, StackTrace property gets a string representation of the frames on the call stack at the time the current exception was thrown and TargetSite property gets the method that throws the current exception. If you use throw, you keep trace information and these properties contain truthful information (I mean stack, and method where exception occurred). On the contrary, usage of throw e causes misinformation about exception occurrence conditions. It occurs because these properties are calculated when you call them instead of storing some value.

Example

Consider the code sample (we use throw without exception object):

using System;

namespace ThrowDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        try
        {
          MethodWithException();
        }
        catch (Exception e)     //first catch
        {
          throw;
        }
      }
      catch (NotImplementedException e)   //second catch
      {
      }
      catch (Exception e)
      {
      }
      finally
      {
      }
    }

    static void MethodWithException()
    {
        throw new NotImplementedException("exception");
    }
  }
}

If you check e inside the first catch, you will see something like

at ThrowDemo.Program.MethodWithException() in D:\Projects\delme4938\delme4938\Program.cs:line 33

at ThrowDemo.Program.Main(String[] args) in D:\Projects\delme4938\delme4938\Program.cs:line 13

in StackTrace and

{Void MethodWithException()}

in TargetSite.

If you check e inside the second catch again, you will see something like

at ThrowDemo.Program.MethodWithException() in D:\Projects\delme4938\delme4938\Program.cs:line 33

at ThrowDemo.Program.Main(String[] args) in D:\Projects\delme4938\delme4938\Program.cs:line 17

in StackTrace and

{Void MethodWithException()}

in TargetSite. What is similar with the previous information.

It reflects reality, doesn't it? Only what the developer should do is go to the method with name 'MethodWithException' in file 'Program.cs' where an exception is raised at line #33. And it is possible to track calls.

Consider another code sample (it is equal with the one above, but throw e is used):

using System;

namespace ThrowDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        try
        {
          MethodWithException();
        }
        catch (Exception e)           //first catch
        {
          throw e;
        }
      }
      catch (NotImplementedException e)     //second catch
      {
      }
      catch (Exception e)
      {
      }
      finally
      {
      }
    }

    static void MethodWithException()
    {
        throw new NotImplementedException("exception");
    }
  }
}

If you check what is inside of e in second catch, you will see something like

at ThrowDemo.Program.Main(String[] args) in D:\Projects\delme4938\delme4938\Program.cs:line 17

in StackTrace and

{Void MethodWithException()}

in TargetSite still.

Thus we lost valuable information which could be useful when you try to investigate what happened. So do yourself a favour - use just throw instead of throw e when you need to throw the exception further. Just throw! :) I mentioned TargetSite property purposely. At least, it could be useful in such case because of stability.

Proviso

Information is saved, but it is a little bit corrupt. Position inside the method where exception was thrown again is shifted from called method at 'throw' clause. You can see that when position inside of Main method transformed from 13 (line with 'MethodWithException' call) to 17 (line with 'throw' call).

History

  • Article was created on 1st of February, 2010