Click here to Skip to main content
Click here to Skip to main content

Exceptions Handling - Some Advanced Detail

, 31 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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! Smile | :) 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

License

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

Share

About the Author

db_developer
Database Developer Freelancer
Ukraine Ukraine
MS SQL Server Database Developer with 7+ years experience
 
Technologies/languages: Business Intelligence, SQL, MDX, VBA, SQL Server, Analysis Services (SSAS), Reporting services (SSRS), Integration Services (SSIS), DataWarehouse.
Also: economic background.
 
Feel free to contact me for rates and details.

Comments and Discussions

 
GeneralInner exceptions Pinmembersupercat91-Feb-10 8:17 
GeneralRe: Inner exceptions PinmemberRoman_K7-Feb-10 17:42 
Generalnot bad Pinmemberkosat1-Feb-10 2:11 
GeneralRe: not bad PinmemberRoman_K1-Feb-10 7:44 
GeneralGood catch... but PinmemberDinesh Mani31-Jan-10 19:35 
GeneralRe: Good catch... but PinmemberRoman_K1-Feb-10 7:54 
GeneralRe: Good catch... but Pinmemberreturn Jonny2-Feb-10 11:15 

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
Web03 | 2.8.141216.1 | Last Updated 31 Jan 2010
Article Copyright 2010 by db_developer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid