Doing a code analysis gave me item CA2200:
CA2200 Rethrow to preserve stack details 'func()' rethrows a caught exception and specifies it explicitly as an argument. Use 'throw' without an argument instead, in order to preserve the stack location where the exception was initially raised.
I have implemented the suggestion, but I seem to get the same stack trace regardless.
Here is my test code and output (the white space is intended to give obvious line numbers):
Expected error at Line 30
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace throw_test
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
// line 22 below
throw new Exception();
}
catch (Exception ex) // DEFINES AND THROWS 'ex'
{
// line 30 below
throw ex;
}
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
Console.Read();
}
}
}
Output
at throw_test.Program.Main(String[] args) in c:\Users\Richard\Documents\Visual Studio 2013\Projects\using_throw_closestream\using_throw_closestream\Program.cs:line 30
Expected error at line 22
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace throw_test
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
// line 22 below
throw new Exception();
}
catch (Exception) // NO 'ex'
{
// line 30 below
throw;
}
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
Console.Read();
}
}
}
Output
at throw_test.Program.Main(String[] args) in c:\Users\Richard\Documents\Visual Studio 2013\Projects\using_throw_closestream\using_throw_closestream\Program.cs:line 30
The same result.
My question
With other such questions suggesting more complex or explicit methods to preserve the stack trace, why does the Code Analysis suggestion ask me to do something which seems not to work?
If this suggestion is in fact correct, what am I doing wrong?
Mark above is correct.
The stack trace is only preserved when thrown from another method.
The stack trace here will be preserved by including the original error being thrown from Method()
The following code illustrates this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace throw_test
{
class Program
{
static void Main(string[] args)
{
try
{
try
{
// line 22 below
Method();
}
catch (Exception ex)
{
// line 30 below
throw;
}
}
catch (Exception ex)
{
Console.Write(ex.StackTrace);
}
Console.Read();
}
public static void Method()
{
throw new Exception();
}
}
}
Output
at throw_test.Program.Method() in c:\Users\Richard\Documents\Visual Studio 2013\Projects\using_throw_closestream\using_throw_closestream\Program.cs:line 43 at throw_test.Program.Main(String[] args) in c:\Users\Richard\Documents\Visual Studio 2013\Projects\using_throw_closestream\using_throw_closestream\Program.cs:line 30