C#
  Home arrow C# arrow Page 2 - C# Simplified, part 5: Error Handling and ...
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# Simplified, part 5: Error Handling and Files
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 15
    2005-05-31

    Table of Contents:
  • C# Simplified, part 5: Error Handling and Files
  • Try-Catch clause
  • Finally clause
  • Accessing files and directories
  • Displaying all files under a directory
  • Creating files
  • Reading from a file
  • Copying a file

  • 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# Simplified, part 5: Error Handling and Files - Try-Catch clause


    (Page 2 of 8 )

     

    Before proceeding to learn the application of the try-catch clause, let’s consider a situation in which the exception is not handled properly. In listing 5.1, an attempt is made to divide a number by zero, which ultimately results in division by zero error.

    Listing 5.1

    using System;

    class WithoutExcep

    {

          public static void Main()

          {

               

                int x = 5;

                int y = 0;

                int z = x/y;

                Console.WriteLine(z);

          }

    }

    If you execute the above program, the C# interpreter produces a dialog box.

    If you click yes, a new instance of Visual Studio .NET debugger will open up, and upon selecting No you will get an output as shown below:

    Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.

       at WithoutExcep.Main()

    In order to avoid this error, you must wrap the portion of the above code which causes trouble with a try clause, and it should be followed by a catch clause. The statements inside the catch block are executed only when an exception occurs. The main advantage to handling this exception is that users are provided with user-friendly error messages, rather than a set of unrecognized statements and dialog boxes. Listing 5.2 is a modified version of listing 5.1. The code properly handles the possible error with a try-catch block.

    Listing 5.2

    using System;

    class WithExcep

    {

          public static void Main()

          {

                try

                {

                      int x = 15;

                      int y = 0;

                      int z = x/y;

                      Console.WriteLine(z);

                }

                catch(Exception)

                {

                      Console.WriteLine("Error occurred, unable to                            compute");

                }

          }

    }

    If you execute the above code, the statement inside the catch block will be printed. It will not produce an error message as you have seen above. Try to replace line 9 with the following code and observe the result:

    int y = 5;

    You can also retrieve system specific error messages with the help of an exception variable, along with your own error message. Listing 5.3 is a modified version of listing 5.2. The only difference is in the catch block, where I have applied a variable named e and called in the statement which encloses the catch block.  

    Listing 5.3

    using System;

    class WithExcep2

    {

          public static void Main()

          {

                try

                {

                      int x = 15;

                      int y = 0;

                      int z = x/y;

                      Console.WriteLine(z);

                }

                catch(Exception e)

                {

                      Console.WriteLine("Error occurred: \n" +e);

                }

          }

    }

    The resulting output of the above code is shown below:

    Error occurred:

    System.DivideByZeroException: Attempted to divide by zero.

       at WithExcep2.Main()

    More C# Articles
    More By Anand Narayanaswamy


     

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