Click here to Skip to main content
15,893,486 members
Articles / Programming Languages / C#

Exceptions Handling - Some Advanced Detail

Rate me:
Please Sign up or sign in to vote.
3.63/5 (9 votes)
31 Jan 2010CPOL2 min read 17.8K   14   7
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):

C#
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):

C#
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

License

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


Written By
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 Pin
supercat91-Feb-10 7:17
supercat91-Feb-10 7:17 
You fail to mention inner exceptions; in many cases, those are the "right" approach to exception handling. The innermost exception in a program may say what went wrong, but often won't really show what the program was trying to do. Using nested exceptions, one may find out that the program filed when inserting a bunch of records into the database, because it threw an exception while updating record #523. That exception was caused by a timeout executing the SQL query. Merely knowing that an SQL query timeout threw an exception somewhere, or merely knowing that the update failed, isn't nearly as good as knowing what failure occurred and what the system was trying to do when it happened.

A potentially tricky issue is what exactly to log with exceptions. In many cases, the query string will be a useful piece of debugging information, but in some cases it might contain confidential data. Showing it in plaintext might be a bad idea.
GeneralRe: Inner exceptions Pin
db_developer7-Feb-10 16:42
db_developer7-Feb-10 16:42 
Generalnot bad Pin
kosat1-Feb-10 1:11
kosat1-Feb-10 1:11 
GeneralRe: not bad Pin
db_developer1-Feb-10 6:44
db_developer1-Feb-10 6:44 
GeneralGood catch... but Pin
Dinesh Mani31-Jan-10 18:35
Dinesh Mani31-Jan-10 18:35 
GeneralRe: Good catch... but Pin
db_developer1-Feb-10 6:54
db_developer1-Feb-10 6:54 
GeneralRe: Good catch... but Pin
return Jonny2-Feb-10 10:15
return Jonny2-Feb-10 10:15 

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

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