0

Possible Duplicate:
incorrect stacktrace by rethrow

Hello,

until now I thought that doing:

int a = 10/0;

will create the same exception as

try
{
    int a = 10/0;
}
catch
{
    throw;
}

but it don't. If you use the second one the stacktrace will contain the throw line as the point where the exception was raised. In this example is not a problem but when you have a real method you don't know where the exception was raised.

This is the MSDN information:

If an exception is re-thrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of method calls between the original method that threw the exception and the current method is lost. To keep the original stack trace information with the exception, use the throw statement without specifying the exception.

Is this really the expected behavior or is this a .Net bug or what? How can I preserve the REAL stacktrace? I could create a new Exception putting ex as the InnerException but that's:

  • Ugly.
  • Longer.
  • Makes you choice the rigth exception to throw.
Community
  • 1
  • 1
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207

0 Answers0