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:
- Finding out those parts from the source code which are most likely to cause runtime errors.
- 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.
Next: Try-Catch clause >>
More C# Articles
More By Anand Narayanaswamy