Simple Exceptions

We all know to organize our try…catch blocks with the most specific exceptions first, right?  Right???  Of course!  (Recommended reading: Exception Management Architecture Guide and Best Practices for Handling Exceptions.)

But how do you actually tell which exceptions are more specific?  Look up the inheritance hierarchy of the exception classes.

You could use the object browser:

Or the MSDN (.NET Framework Class Library Online Reference):

How do you tell which exceptions you need?  Look up the .NET FCL classes in the MSDN .NET Framework Class Library Online Reference.  The class description usually tells you which exceptions to watch for.  For example, the "The HttpWebRequest class throws a WebException when errors occur while accessing a resource."

Here's a quick code example.  Now, you only want to include the exceptions that could actually occur in your code.  I put a lot of exceptions in here just to make a point.

public static string DownloadFileContents(string fileUrl)
{
    string contents = null;
    StreamReader reader =
null;
    HttpWebRequest webRequest;
    HttpWebResponse webResponse =
null;

    try 
    {
        webRequest = WebRequest.Create(fileUrl)
as HttpWebRequest;
        if (webRequest != null)
        {
            webResponse = webRequest.GetResponse()
as HttpWebResponse;
            if (webResponse != null)
            {
                reader =
new StreamReader(webResponse.GetResponseStream());
                contents = reader.ReadToEnd();
            }
        }
    }

    // inheritance hierarchy = WebException : InvalidOperationException : SystemException : Exception 
    // this will catch all unhandled exceptions that are of type WebException or inherit from it
    catch
(System.Net.WebException ex)
    {
        //Log and handle exception
    }

    // inheritance hierarchy = InvalidOperationException : SystemException : Exception 
    // this will catch all unhandled exceptions that are of type InvalidOperationException or inherit from it
    catch (System.InvalidOperationException ex)
    {
        //Log and handle exception
    }

    // inheritance hierarchy = SecurityException : SystemException : Exception 
    // this will catch all unhandled exceptions that are of type SecurityException or inherit from it
    catch
(System.Security.SecurityException ex)
    {
        //Log and handle exception
    }

    // inheritance hierarchy = ArgumentException : SystemException : Exception
    // this will catch all unhandled exceptions that are of type ArgumentException or inherit from it
    catch
(ArgumentException ex)
    {
        //Log and handle exception
    }

    // inheritance hierarchy = OutOfMemoryException : SystemException : Exception
    // this will catch all unhandled exceptions that are of type OutOfMemoryException or inherit from it
    catch
(OutOfMemoryException ex)
    {
        //Log and handle exception
    }

    // inheritance hierarchy = SystemException : Exception
    // this will catch all unhandled exceptions that are of type SystemException or inherit from it
    catch
(System.SystemException ex) 
    {
        //Log and handle exception
    }

    // inheritance hierarchy = Exception
    // this will catch all unhandled exceptions

    catch (Exception ex) 
    {
        //Log and handle exception
    }

    finally 
    {
        if (reader != null
        {
            reader.Close();
        }
        if (webResponse != null
        {
            webResponse.Close();
        }
    }

    return contents;
}

posted on Wednesday, September 27, 2006 7:57 PM by snyholm

Comments