C#
  Home arrow C# arrow Page 6 - 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# - Using Statements


    (Page 6 of 8 )

    In C#, the nearest you can get to the "ideal" version is this:


    private static char[] ReadSource(string filename)
    {
        FileInfo file 
    = new FileInfo(filename);
        int length 
    = (int)file.Length;
        char
    [] source = new char[length];
        using 
    (TextReader reader file.OpenText())
        
    {
            reader
    .Read(source0length);
        
    }
        
    return source;
    }

    This is pretty close. And as I'll explain shortly it has a number of features that improve on the "ideal" version. But first let's look under the lid to see how it actually works.

    Using Statement Translation

    The C# ECMA specification states that a using statement:


    using (type variable initialization)
        embeddedStatement
    is exactly equivalent to

    {
        type variable 
    initialization;
        try
        
    {
            embeddedStatement
        
    }
        finally
        
    {
            
    if (variable != null
            
    {
                
    ((IDisposable)variable).Dispose();
            
    }
        
    }
    }

    This relies on the IDisposable interface from the System namespace:


    namespace System
    {
        
    public interface IDisposable
        
    {
            void Dispose
    ();
        
    }
    }

    Note that the cast inside the finally block implies that variable must be of a type that supports the IDisposable interface (either via inheritance or conversion operator). If it doesn't you'll get a compile time error.

    Using TextReader Translation

    Not surprisingly, TextReader supports the Disposable interface and implements Dispose to call Close. This means that this:


    using (TextReader reader file.OpenText())

        reader
    .Read(source0length);
    }

    is translated, under the hood, into this:


    {
        TextReader reader 
    file.OpenText();
        try
        
    {
            reader
    .Read(source0length);
        
    }
        finally
        
    {
            
    if (reader != null)
            
    {
                
    ((IDisposable)reader).Dispose();
            
    }
        
    }
    }

    Apart from the cast to IDisposable this is identical to the best general Java solution. The cast is required because this is a general solution.

    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