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

C# Exceptions
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-10-09

    Table of Contents:
  • C# Exceptions
  • The finally Block
  • Throwing Exceptions
  • Key Properties of System.Exception

  • 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


    C# Exceptions - Throwing Exceptions


    (Page 3 of 4 )

    Exceptions can be thrown either by the runtime or in user code. In this example, Display throws a System.ArgumentNullException:

      class Test
      {
        static void Display (string name)
        {
         
    if (name == null)
          
    throw new ArgumentNullException ("name");

          Console.WriteLine (name);
        }

        static void Main()
       
    {
          try { Display (null); }
          catch (ArgumentNullException ex)
          {
           
    Console.WriteLine ("Caught the exception");
          }
        }
      }

    Rethrowing an exception

    You can capture and rethrow an exception as follows:

      try { ... }
      catch (Exception ex)
      {
       
    // Log error
        ...
       
    throw;    // Rethrow same exception
      } 

    Rethrowing in this manner lets you log an error without swallowing it. It also lets you back out of handling an exception should circumstances turn out to be outside what you expected:

      using System.Net;      // (See Chapter14)
      ...

      string s;
     
    using (WebClient wc = new WebClient())
        try { s = wc.DownloadString ("http://albahari.com/"); }
        catch (WebException ex)
        {
         
    if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            Console.WriteLine ("Bad domain name");
          else
           
    throw;    // Can't handle other sorts of WebException, so rethrow
        
    }

    The other common scenario is to rethrow a more specific exception type. For example:

      try
      {
       
    ... // parse a date of birth from XML element data
      }
      catch (FormatException ex)
      {
       
    throw new XmlException ("Invalid date of birth", ex);
      }

    Rethrowing an exception does not affect theStackTraceproperty of the exception (see the next section). When rethrowing a different exception, you can set theInnerExceptionproperty with the original exception if doing so could aid debugging. Nearly all types of exceptions provide a constructor for this purpose.

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.

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