Throwing exception question...


Author
Message
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Yes, it does also apply to VB.NET.  You can use Throw without specifying the exception to keep it from clearing the stack trace.
StarkMike
StarkMike
StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)StrataFrame User (448 reputation)
Group: Forum Members
Posts: 436, Visits: 944
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
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search