C#
  Home arrow C# arrow C# Simplified, part 5: Error Handling and ...
Click Here
Iron Speed
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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 14
    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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    C# Simplified, part 5: Error Handling and Files


    (Page 1 of 8 )

    In this article, you will learn about the two types of errors and how to handle them. You will also learn how to access files and directories, and a number of useful things you can do with those files once you have accessed them.C# Simplified covers each and every concept of C# programming language in a concise manner. The articles in this series have been divided into several parts and will provide detailed explanations along with source codes and screenshots. This series has been specifically written for beginners and students with an aim to teach C# in a quickest possible time. Please send your comments to csharpsimplified@gmail.com

    Not all programmers are 100 percent accurate. Errors can happen and sometimes they prove fatal. As a programmer, you have to carefully handle each and every possible error. As you may know, there are two types of errors. They are compilation and runtime. Compilation errors can be rectified during the programming phase itself. These errors can happen due to bad coding, misspelling of syntaxes and so on. They can be corrected by studying the error messages and warnings produced by the compiler.

    On the other hand, runtime errors are very crucial. They will occur at the time of program execution and cannot be corrected. These errors are referred to as exceptions. A classic example of an exception is the division by zero error. If you are developing a calculator, you must provide a system for handling this error. Another classic example of an exception is overflow of arrays. Hence these kinds of errors cannot be avoided.

    You as a programmer should take proper measures to avoid these exceptions at the time of coding the program. Every programming language provides some sort of technique for handling runtime errors.

    You must identify the following two key aspects before starting to program with C#. They are:

    1. Finding out those parts from the source code which are most likely to cause runtime errors.
    2. Handling those errors according to C# language conventions.

    As you may be aware, the base class of all exception classes in the .NET Framework is System.Exception. This class defines numerous exceptions, which are used for different purposes. Table 5.1 lists some of the important exceptions defined under this class.

    Name

    Description of probable cases

    ArithmeticException

    Data Type not matched, invalid casting etc.

    DivideByZeroException

    An attempt to divide a value by zero.

    FormatException

    Incorrect arguments for the methods.

    MissingMethodException

    An attempt to call an invalid method.

    OutOfMemoryException

    Not enough memory to complete an operation.

    OverflowException

    An attempt to give large values to a variable of some type.

    Different ways to handle exceptions         

    Exceptions in C# must be handled in a special manner, with the help of try-catch blocks and according to the C# language conventions.  

    You can also apply the finally{} clause, but this is optional. Furthermore, you can create your own exceptions with the help of the throw keyword. The try block will appear only once in a program, but the catch block can appear one or more times. This is because you may have to handle more than one exception in a single program. The code inside the finally block will always execute, whether the exception has occurred or not.

    More C# Articles
    More By Anand Narayanaswamy


     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway