Exceptions in C#
(Page 1 of 4 )
.NET exception handling is a unified approach to error handling. A developer is no longer required to write complex code in order to account for every error scenario. Instead, the code in question is wrapped with a try block, followed by a list of exceptions that the code can cause. The exception caught is a class that is filled with valuable information about the error.
Before we start with what exceptions are and how to use them in the .NET Framework, let's take a quick look at how we used to handle errors in other programming languages (like C):
int main()
{
int result = MyFunction();
//assume ERROR_FILE_NOT_FOUND is already defined
if(result == ERROR_FILE_NOT_FOUND)
printf("Error: file not found");
}
int MyFunction()
{
...
//the file was not found and we need to return an error
return ERROR_FILE_NOT_FOUND
}
Looking at this code, we can find at least two problems:
The constant ERROR_FILE_NOT_FOUND is merely a number. It does not give us enough information about the error and how to deal with it.
In case we need to handle more errors, we have to build more and more embedded ifs to account for all scenarios. This leads to complicated and unstructured code.
To solve this issue, the .NET Framework provides a more structured approach to error handling. This approach is the same for all programming languages under the .NET Framework (C#, VB.NET...). Knowing that .NET languages are object-oriented, it should not come as a surprise that exceptions are classes. Every Exception class encapsulates all the necessary information about an exception (even its inner exception that we will explore shortly). This information includes:
A help link to a help file associated with the exception
A descriptive message
The source (the name of the application or the object that causes the error)
A stack trace of the method calls that lead to the exception
These bits of information are bundled together to give us a more meaningful interpretation of what exactly happens and where. Armed with these exceptions, developers can easily catch many errors and handle them gracefully.
The .NET Framework provides a list of exceptions that you can use in your applications. Examples of these exceptions include IndexOutOfRangeException, IOException, FileNotFoundException, etc. All exceptions are in an inheritance hierarchy leading to the top where the Exception class lives (Exception is the parent of all exceptions). Exceptions come in two categories: ApplicationException and SystemException.
SystemException: This exception is thrown by the common language runtime when recoverable errors occur (such as an array out-of-bounds error).
ApplicationException: This exception is thrown by a user program, not the runtime. Inherit from this class when designing your own exception hierarchy.
C# provides four keywords that deal with exceptions: try, catch, throw, and finally. Before we discuss those keywords, let's start with an example that intentionally throws an exception and work our way through to handling the exception. One good example will be accessing an element outside the bounds of an array:
class Program
{
static void Main( string [] args)
{
int [] a = new int [3];
a[0] = 10; //ok
a[1] = 20; //ok
a[2] = 30; //ok
a[3] = 40; //No such element.
}
}
Here is a visual interpretation:
a[0] a[1] a[2] a[3]
Next: Visual Explained >>
More C# Articles
More By Ayad Boudiab