C#
  Home arrow C# arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 23
    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#


    (Page 1 of 8 )

    The fundamental thing that exceptions allow you to do is to separate the essential functionality from the error handling. ReadSource has come close to the ideal version we're all looking for, thanks to exceptions. Ironically, exceptions allow us to get close to this ideal version of ReadSource but at the same time prevent us from quite reaching it. 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 to this lost release problem depends on the language you're using. C# allows you to create user-defined struct types that live on the stack but does not allow struct destructors.

    Painful Procedural Error Handling

    In the absence of exceptions the classic way to handle errors is to intertwine your statements with error checks. For example:


    public sealed class Painful
    {  
        
    ...  
        
    private static char[] ReadSource(string filename)
        
    {
            FileInfo file 
    = new FileInfo(filename);
            
    if (errorCode == 2342goto handler;
            int length 
    = (int)file.Length;  
            char
    [] source = new char[length];
            
    if (errorCode == -734goto handler;
            TextReader reader 
    file.OpenText();
            
    if (errorCode == 2664goto handler;
            reader
    .Read(source0length);
            
    if (errorCode == -5227goto handler;
            reader
    .Close();
            Process
    (filenamesource); 
            
    return source;
            
    handler:
            
    ...
        
    }
    }

    This style of programming is tedious, repetitive, awkward, complex, and obscures the essential functionality. And it's too easy to ignore errors (either deliberately or accidentally). There have to be better ways. And there are. But some are better than others.

    More C# Articles
    More By Jon Jagger


     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek