StrataFrame Forum

Throwing exception question...

http://forum.strataframe.net/Topic2170.aspx

By StarkMike - 8/11/2006

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
By StrataFrame Team - 8/11/2006

Yes, it does also apply to VB.NET.  You can use Throw without specifying the exception to keep it from clearing the stack trace.