StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



Throwing exception question...Expand / Collapse
Author
Message
Posted 08/11/2006 4:21:02 PM


StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: 07/09/2008 2:20:16 PM
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
Post #2170
Posted 08/11/2006 4:44:33 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Yesterday @ 5:02:56 PM
Posts: 2,682, Visits: 1,882
Yes, it does also apply to VB.NET.  You can use Throw without specifying the exception to keep it from clearing the stack trace.


www.bungie.net
Post #2173
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ben Chase, Trent L. Taylor, Steve L. Taylor

PermissionsExpand / Collapse

All times are GMT -6:00, Time now is 6:55pm

Powered by InstantForum.NET v4.1.4 © 2008
Execution: 0.047. 10 queries. Compression Enabled.
Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.