Raba - Defend your code RSS 2.0
# Sunday, January 29, 2006

Today, while I was sitting with Matan, a new programmer at our team, for a code review - I saw one of the worst practices that each new programmer and even experienced programmers are dealing with during their coding/reading in .Net, the Exception rethrow mechanism.

I would like to show here two code examples for best and worst practices:
I wrote this method for further usage:

static void ThrowNotImplementedException ()
{
   throw new NotImplementedException("not implemented causes in here");
}

Worst Practice Example:

try 
{
   ThrowNotImplementedException();
}
catch (NotImplementedException nie)
{
   nie.HelpLink = "I am here!!!";
   throw nie;
}

Best Practice Example:

try 
{
   ThrowNotImplementedException();
}
catch (NotImplementedException nie)
{
   nie.HelpLink = "I am here!!!";
   throw;
}

If you are looking at those code snippets and say to yourself: what? this one is for beginners, so first - I agree with you, secondly stop talking to yourself!
but still there are many programmers that don't remember, or don't even know this case, I want to show you the difference between those two cases:

stack trace results:
Worst Practice:

Best Practice:

Pay attention to the difference between the stack traces which the worst practice do not include the method that caused the Exception! Enjoy debugging.

Last minute editing:
I opened the ILDASM (my favorite tool) and looked at the difference between those two:
throw x;  will convert into assembly command named throw.
throw ;   will convert into assembly command named rethrow.

I hope this article will help you remember the differences.

Sunday, January 29, 2006 11:44:02 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
.Net

Archive
<January 2006>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Shani Raba
Sign In
Statistics
Total Posts: 145
This Year: 0
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2012, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)