Spot the web RSS 2.0
# Tuesday, December 04, 2007

This is one of those "Yeah I've always seen people use it differently but I don't know why!" questions.

Re-throwing exceptions can be misused, although it may not cause any harm to your application - there are multiple ways of re-throwing an exception, most likely for the purpose of bubbling it up to a higher level.

Lets look at how many ways we can use throw:

    1. throw
    2. throw ex
    3. throw new Exception(); 

You should not use #3 except if you are throwing a specific exception other that the one that was fired such as a custom exception. To re-throw an exception in .NET your best option is #1 instead of #2 and that is simply because of how they stack up in the stack trace. Lets take a look at the stack trace for both instances using the following snippet.

 

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DoMath();
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// A method that simply calls another method.
    /// </summary>
    /// <remarks>
    /// Just a helper to show stack trace
    /// </remarks>
    private void DoMath()
    {
        MethodWithError();
    }

    /// <summary>
    /// This method will throw an error
    /// </summary>
    private void MethodWithError()
    {
        throw new Exception("Generic exception");
    }

 

The stack trace looks like this:

[Exception: Generic exception]
   throwerror.Page_Load(Object sender, EventArgs e) 
in d:\My Websites\demoweb\throwerror.aspx.cs:22
   System.Web.Util.CalliHelper.EventArgFunctionCaller
(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback
(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
Boolean includeStagesAfterAsyncPoint) +1061

 

However, if I replace "throw ex;" with just "throw;", here is the new stack trace.

[Exception: Generic exception]
   throwerror.MethodWithError() in d:\My Websites\demoweb\throwerror.aspx.cs:42
   throwerror.DoMath() in d:\My Websites\demoweb\throwerror.aspx.cs:34
   throwerror.Page_Load(Object sender, EventArgs e) in d:\My Websites\demoweb\throwerror.aspx.cs:22
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

 

Its all about the details. Obviously, using just "throw;" gives us much more details - so use "throw" to re-throw an error.

Tuesday, December 04, 2007 5:10:06 PM (Jerusalem Standard Time, UTC+02:00)  #    Comments [0] - Trackback
.Net | Visual Studio | Programming

Now that the .NET Framework 3.5 has been released, this version includes logic to detect the presence of the .NET Framework 3.5 and also .NET Framework 3.0 service packs, which was missing from previous versions of this sample code.

You can download updated versions of the sample code at the following locations:

For reference, the registry locations used in this sample code to detect the .NET Framework 3.0 service pack level and the .NET Framework 3.5 are listed below.

To detect the .NET Framework 3.0 service pack level:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0]
SP = <value>

Note - this SP value will not exist at all if you only have the original release of the .NET Framework 3.0.  This value was added starting in the .NET Framework 3.0 SP1 and will be updated for future service packs.

To detect the .NET Framework 3.5 final release:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5]
Install = 1
Version = 3.5.21022.08

To detect the .NET Framework 3.5 service pack level:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5]
SP = <value>

Note - this SP value does exist in the final release of the .NET Framework 3.5, and will be set to 0.  Future service packs will increment this value as appropriate.

Tuesday, December 04, 2007 5:01:18 PM (Jerusalem Standard Time, UTC+02:00)  #    Comments [0] - Trackback
.Net | Microsoft | Visual Studio
# Monday, December 03, 2007

What is Snooth (http://www.snooth.com/)?

Snooth is a web-based social shopping experience that is simplifying how people select, interact with and purchase their favorite wines. It hopes to become a kind of Facebook for the wine world. It may not sell wine but it already has a database of more than one million reviews, from the public and established critics.

Snooth offers both casual and aspiring wine drinkers personalized wine recommendations, ratings & reviews, as well as a wine information search tool that seamlessly connects users to the websites of top online merchants and wineries worldwide.

The main screen:

Search results screen:

Monday, December 03, 2007 2:03:37 PM (Jerusalem Standard Time, UTC+02:00)  #    Comments [0] - Trackback
Site Reviews | Web 2.0
Navigation
Archive
<December 2007>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
About the author/Disclaimer

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

© Copyright 2012
Guy Levin
Sign In
Statistics
Total Posts: 63
This Year: 0
This Month: 0
This Week: 0
Comments: 14
Themes
All Content © 2012, Guy Levin