Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throwing exceptions question... 1

Status
Not open for further replies.

sqlsamurai

Programmer
May 11, 2005
120
US
Here is an excerpt from a book called: "Visual Studio .NET Tips and Tricks" By Minh T. Nguyen

Rethrowing the Same Exception

Whenever an exception needs to be logged, developers tend to use code similar to the
following to log an exception without eating it up:

Try
{...}
catch (Exception ex) {
Log(ex);
throw ex;
}

This code works, and the exception won’t be eaten up because it’s being rethrown at the end. However, developers don’t often realize that rethrowing the same exception in this way causes .NET to clear the exception’s stack trace. When you inspect the exception’s stack trace property it will seem as though the exception was originally thrown for the first time inside that catch block.

To throw the same exception correctly in a catch block, simply use “throw;” as follows:
Try
{...}
catch (Exception ex) {
Log(ex);
throw;
}

This rethrows the same exception that was just caught without clearing the stack trace.

His example is in C++ or C#... does what he's saying also apply to VB .NET?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top