Alternative Method of Throwing Exception

Most of us use Exceptional Handling mechanism to prevent our programs from crashing and then debug the program by understanding the message thrown.

Traditional way to use would be to use try..catch..finally block to handle the exceptions thrown using throw. How do you throw exceptions? There are basically two ways to throw exception.

throw new Exception();

Or simply,

throw;

Most of us would be using the first method to throw. But sometimes, it might not contain enough information that is required to solve our problems (bug). In that case, the second method would be more useful.

Below is an example of the c# code that would help to distinguish between the two types of errors.

There are two methods defined in the Program class under ConsoleApplication1 namespace. (I’m assuming the file named a.txt doesn’t exist in c drive)

The first method uses throw ex;

private void LoadFile()
{
  try
  {
    using (FileStream fs = new FileStream("c:\\a.txt", FileMode.Open, FileAccess.Read))
    {
    }
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

The second method uses only throw; (You can enter this only in the catch block).

private void LoadFile2()
{
  try
  {
    using (FileStream fs = new FileStream("c:\\a.txt", FileMode.Open, FileAccess.Read))
    {
    }
  }
  catch (Exception ex)
  {
    throw;
  }
}

The program is as follows:

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{

  class Program
  {
    static void Main(string[] args)
    {
      Program p = new Program();

      try
      {
        p.LoadFile();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.StackTrace);
      }

      Console.WriteLine();

      try
      {
        p.LoadFile2();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.StackTrace);
      }
    }

    private void LoadFile()
    {
      // code omitied.
    }


    private void LoadFile2()
    {
      // code omited.
    }
  }
}

When you run the program you will get the following output.

The error message printed to the console is as follows:

at ConsoleApplication1.Program.LoadFile() in D:\Prabir\Documents\Visual Studi
o 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 47
at ConsoleApplication1.Program.Main(String[] args) in D:\Prabir\Documents\Vis
ual Studio 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at ConsoleApplication1.Program.LoadFile2() in D:\Prabir\Documents\Visual Stud
io 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 62
at ConsoleApplication1.Program.Main(String[] args) in D:\Prabir\Documents\Vis
ual Studio 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 28
Press any key to continue . . .

As you might have noticed the second information is in more details. This allows you to know your stack trace in more details.

So how is it different?

In the first method, the stack trace is initialized at throw ex statement. The second method rethrows the exception meaning that the previous stack trace is kept along with more information where they occurred.

ThrowRethrowExample_CSCode.zip (538.00 bytes)