C#
  Home arrow C# arrow Exceptions 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#

Exceptions in C#
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-17

    Table of Contents:
  • Exceptions in C#
  • Visual Explained
  • InnerException
  • The Finally Block

  • 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


    Exceptions in C#


    (Page 1 of 4 )

    .NET exception handling is a unified approach to error handling. A developer is no longer required to write complex code in order to account for every error scenario. Instead, the code in question is wrapped with a try block, followed by a list of exceptions that the code can cause. The exception caught is a class that is filled with valuable information about the error.

    Before we start with what exceptions are and how to use them in the .NET Framework, let's take a quick look at how we used to handle errors in other programming languages (like C):


    int main()

    {

    int result = MyFunction();

    //assume ERROR_FILE_NOT_FOUND is already defined

    if(result == ERROR_FILE_NOT_FOUND)

    printf("Error: file not found");

    }

     

    int MyFunction()

    {

    ...

    //the file was not found and we need to return an error

    return ERROR_FILE_NOT_FOUND

    }


    Looking at this code, we can find at least two problems:

    1. The constant ERROR_FILE_NOT_FOUND is merely a number. It does not give us enough information about the error and how to deal with it.

    2. In case we need to handle more errors, we have to build more and more embedded ifs to account for all scenarios. This leads to complicated and unstructured code.

    To solve this issue, the .NET Framework provides a more structured approach to error handling. This approach is the same for all programming languages under the .NET Framework (C#, VB.NET...). Knowing that .NET languages are object-oriented, it should not come as a surprise that exceptions are classes. Every Exception class encapsulates all the necessary information about an exception (even its inner exception that we will explore shortly). This information includes:

    • A help link to a help file associated with the exception

    • A descriptive message

    • The source (the name of the application or the object that causes the error)

    • A stack trace of the method calls that lead to the exception

    These bits of information are bundled together to give us a more meaningful interpretation of what exactly happens and where. Armed with these exceptions, developers can easily catch many errors and handle them gracefully.

    The .NET Framework provides a list of exceptions that you can use in your applications. Examples of these exceptions include IndexOutOfRangeException, IOException, FileNotFoundException, etc. All exceptions are in an inheritance hierarchy leading to the top where the Exception class lives (Exception is the parent of all exceptions). Exceptions come in two categories: ApplicationException and SystemException.

    • SystemException: This exception is thrown by the common language runtime when recoverable errors occur (such as an array out-of-bounds error).

    • ApplicationException: This exception is thrown by a user program, not the runtime. Inherit from this class when designing your own exception hierarchy.

    C# provides four keywords that deal with exceptions: try, catch, throw, and finally. Before we discuss those keywords, let's start with an example that intentionally throws an exception and work our way through to handling the exception. One good example will be accessing an element outside the bounds of an array:


    class Program

    {

    static   void Main( string [] args)

    {

    int [] a = new   int [3];

    a[0] = 10; //ok

    a[1] = 20; //ok

    a[2] = 30; //ok

    a[3] = 40; //No such element.

    }

    }


    Here is a visual interpretation:

    10

    20

    30

    40

    a[0]         a[1]         a[2]      a[3]

    More C# Articles
    More By Ayad Boudiab


       · Hello Everyone,Here is my latest article on how to handle exceptions in C#....
     

    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 2 hosted by Hostway
    Stay green...Green IT