C#
  Home arrow C# arrow Page 2 - Exception Handling in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Exception Handling in C#
By: Jon Jagger
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 22
    2004-01-28

    Table of Contents:
  • Exception Handling in C#
  • Separation of Concerns
  • The Solution, but More Issues
  • A Second Attempt
  • Fourth Time's
  • Using Statements
  • Do it Yourself?
  • struct Alternative

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Exception Handling in C# - Separation of Concerns


    (Page 2 of 8 )

    The fundamental thing that exceptions allow you to do is to separate the essential functionality from the error handling. In other words, we can rewrite the mess above like this:


    ...
    public sealed 
    class PainLess
    {    
        
    public static int Main(string[] args)
        
    {
            try
            
    {
               string filename 
    args[0];
               char
    [] source ReadSource(filename);
               Process
    (filenamesource);
               
    return 0;
            
    }
            
    catch (SecurityException    caught) { ... }
            
    catch (IOException          caught) { ... }
            
    catch (OutOfMemoryException caught) { ... }
            
    ...
        
    }
        private static 
    char[] ReadSource(string filename)
        
    {
            FileInfo file 
    = new FileInfo(filename);
            int length 
    = (int)file.Length;
            char
    [] source = new char[length];
            TextReader reader 
    file.OpenText();
            reader
    .Read(source0length);
            reader
    .Close();
            
    return source;
        
    }
    }

    There are several things to notice about this transformation.

    1. The numeric integer error codes that utterly failed to describe the errors they represented (e.g. what does 2342 mean?) are now descriptively named exception classes (e.g. SecurityException).

    2. The exception classes are not tightly coupled to each other. In contrast, each integer code must hold a unique value thus coupling all the error codes together.

    3. There is no throw specification on ReadSource. C# does not have throw specifications.

    However, by far and away the most important thing to notice is how much cleaner, simpler and easier to understand ReadSource has become. It now contains only the statements required to implement its essential functionality. It makes no apparent concession to error handling. This is possible because if an exception occurs, the call stack will unwind all by itself. This version of ReadSource is the "ideal" we are aiming it. It is as direct as we can make it.

    Ironically, exceptions allow us to get close to this ideal version of ReadSource but at the same time prevent us from quite reaching it. ReadSource is an example of code that acquires a resource (a TextReader), uses the resource (Read), and then releases the resource (Close). The problem is that if an exception occurs after acquiring the resource but before releasing it then the release will not take place. The solution has become part of the context. Nevertheless, this ideal version of ReadSource is useful; we can compare forthcoming versions of ReadSource to it as a crude estimate of their idealness.

    More C# Articles
    More By Jon Jagger


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT