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


    (Page 3 of 8 )

    You have already seen from the above explanations that the statements inside the catch block will be executed only if an error occurs. However, there may be situations where something should be performed whether an exception has occurred or not. In such cases, you can make use of the finally clause, as shown in listing 5.4:

    Listing 5.4

    using System;

    class FinallyDemo

    {

          public static void Main()

          {

                try

                {

                    int x = 15;

                    int y = 0;

                    int z = x/y;

                    Console.WriteLine(z);

                    }

                catch(DivideByZeroException e)

                {

                    Console.WriteLine("Error occurred " +e);

                    }

                finally

                {

                    Console.WriteLine("Thank you for using the program");

                }

          }

    }

    In the above code, the statement inside the finally block will always execute. The final output looks like this:

    Error occurred System.DivideByZeroException: Attempted to divide by zero.

       at FinallyDemo.Main()

    Thank you for using the program

    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