C#
  Home arrow C# arrow Page 3 - 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# - InnerException


    (Page 3 of 4 )


    One of the important properties of the Exception class is InnerException. As the term implies, it is an exception within an exception. Here is an example that illustrates its usage:


    class   Program

    {

    static   void Main( string [] args)

    {

    int [] a = new   int [3];

    try

    {

    PopulateArray(a);

    }

    catch ( Exception e)

    {

    //printing the exception details

    WriteExceptionDetails(e);

    }

    }

    static   void PopulateArray( int [] a)

    {

    try

    {

    a[0] = 10; //ok

    a[1] = 20; //ok

    a[2] = 30; //ok

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

    }

    catch ( Exception e)

    {

    Exception e2 = new   Exception ( "Array access exception" , e);

    throw e2;

    }

    }

    static   void WriteExceptionDetails( Exception e)

    {

    Console .WriteLine( "Message: {0}" , e.Message);

    Console .WriteLine( "Source: {0}" , e.Source);

    Console .WriteLine( "InnerException: {0}" , e.InnerException);

    Console .WriteLine( "StackTrace: {0}" , e.StackTrace);

    Console .WriteLine( "TargetSite: {0}" , e.TargetSite);

    }

    }


    Notice the code in the PopulateArray() method:


    Exception e2 = new   Exception ( "Array access exception" , e);

    throw e2;


    The Exception constructor is overloaded to accept a message and another exception (the inner exception). We take advantage of this constructor by recording the exception that happened in PopulateArray(). Then, we can throw e2 (with its inner exception) to be caught by the calling method ( Main() ). After Main() catches the exception in its catch block, it writes the exception details to the screen (including the inner exception from PopulateArray()) by calling WriteExceptionDetails().

    Since there are many types of exceptions that can be thrown, it comes as no surprise that we can have many catch blocks. Every catch block handles the type of exception that we expect to be thrown. Ponder the following code:


    class Program

    {

    static   void Main( string [] args)

    {

    int [] a = new   int [3];

    try

    {

    a[0] = 10; //ok

    a[1] = 20; //ok

    a[2] = 30; //ok

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

    }

    catch ( IndexOutOfRangeException e)

    {

    Console .WriteLine( "An index out of range exception has

    occurred!" );

    }

    catch ( ArgumentException e)

    {

    Console .WriteLine( "An argument exception has occurred!" );

    }

    catch ( ArrayTypeMismatchException e)

    {

    Console .WriteLine( "An array type mismatch exception has

    occurred!" );

    }

    catch ( Exception e)

    {

    //printing the exception details

    WriteExceptionDetails(e);

    }

    }


    static   void WriteExceptionDetails( Exception e)

    {

    Console .WriteLine( "Message: {0}" , e.Message);

    Console .WriteLine( "Source: {0}" , e.Source);

    Console .WriteLine( "InnerException: {0}" , e.InnerException);

    Console .WriteLine( "StackTrace: {0}" , e.StackTrace);

    Console .WriteLine( "TargetSite: {0}" , e.TargetSite);

    }

    }


    IndexOutOfRangeException is the best match for the type of exception that occurred in this code. Therefore, the output of the program will be: "An index out of range exception has occurred." The order of the catch blocks matters; we catch exceptions from the most specific to the most general. As a matter of fact, the compiler will issue an error message if you try to catch a more general exception before a specific exception (try to move catch (Exception e) and place it before catch (IndexOutOfRangeException e) and see what happens).

    More C# Articles
    More By Ayad Boudiab


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

    C# ARTICLES

    - 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#
    - 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#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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