• Fabian Wetzel
  • 2013-12-16
  • 1 min. read

Writing 'throw ex;' is almost always wrong!

So what is the difference between throw ex; and throw; in C#? The first statement recreates the stack trace while the latter one preserves it. Consider the following method, which will throw an exception if it gets called:

1
2
3
4
private static void Inner()
{
throw new Exception();
}

And then you have two different methods implementing the two different coding styles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private static void Rethrow()
{
try
{
Inner();
}
catch (Exception ex)
{
throw; //this is a REthrow
}
}

private static void ThrowEx()
{
try
{
Inner();
}
catch (Exception ex)
{
throw ex;//this is a second throw
}
}

At last, you have a short console application to call both methods and print their stack trace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static void Main(string[] args)
{
try
{
** ThrowEx();**
}
catch (Exception ex)
{
PrintEx(ex);
}
//...
try
{
** Rethrow();**
}
catch (Exception ex)
{
PrintEx(ex);
}
}

Here is the output:

image

As you can see, throw; preserves the actual location, where the exception really was thrown.

There is hardly any case where you want to hide the actual location of an error, so the best is to just use throw; as your default in a catch clause.