Exception Class and Properties in C#

Exception handling is crucial since the robustness of software depends on how effectively a program deals with exceptions. If you have ever developed software, you realize that code requires intensive exception handling. Even well-written lines of code fail and cause errors, so there must be a mechanism to capture and deal with such failures. Like C++ and Java, C# provides exception handling. So for you, exception handling is a matter of planning for the handling of failures.

Why Exception Handling? Let's assume that your program doesn't have an exception-handling mechanism. What would happen if it tries to access a floppy drive that doesn't contain a disk? Similarly, if you have a lot of open applications, will the program be able to handle an "out of memory" error? In these cases, there is nothing a program can do to prevent these errors from occurring-they're out of its control. These are examples of an abnormal execution of an application.

Have you ever received a divide-by-zero error, a stack-overflow error, or a type-mismatch error? These errors occur when a calling program makes mistakes in passing arguments or when it does not check error conditions-in other words, erroneous execution. When these kinds of errors occur, the program should be able to capture the error and exit gracefully or continue normally. As in the floppy disk example, the program could capture the error and ask the user to retry the operation. These examples show why the exception handling mechanism is so important to software developers.

Exception Handling in C#

Exception handling in C# provides a uniform, structured, and type-safe way of handling errors. (We will see the details of these features later in this chapter.) The exception mechanism in C# is very similar to the one in C++, including these notable features:

  • All exceptions are derived from System.Exception, allowing type-safe handling of exceptions.
  • Well-defined exception classes exist for system-level errors-such as overflow, divide-byzero, and null dereferences-which are treated on par with application-level errors.
  • C# contains inner exceptions. When a new exception is thrown from an existing exception, the new exception's inner exception can carry the existing exception.
  • Checked and unchecked statements are used to control overflow checking.
  • An exception raised in VB.NET but handled in a C# program is handled in the same way as one raised in C# but handled in VB.NET. There is a uniform way of handling errors.

C# provides a structured way of handling errors: the block of code where errors are expected is guarded by try-catch blocks where exceptions are handled. (Try-catch and block statements will be covered in more detail in the following sections.)

System.Exception Overview

System.Exception represents errors that occur during application execution. This is the base class for all exceptions in C#, so any other type of exception must be derived from it. When an error occurs, either the system or the currently executing application reports it by throwing an exception containing information about the error. The Exception class is present in the System namespace and in the assembly mscorlib.dll.

Some Important Properties of Exception class

In this section we'll focus on properties of the Exception class that we can use in the application for exception handling. The Exception class properties below will make understanding exceptions easier.

StackTrace

This StackTrace property can be used to determine where the error occurred. Examples in the following sections use the StackTrace property to display the location of the error in the code.

Message

The Message property returns a description of the error's cause. Examples in the following sections use the Message property to display a description of error message to the user.

InnerException

The InnerException property can be used to create and preserve a series of exceptions during exception handling. When there are series of exceptions, the most current exception can be used to obtain the prior exception through the InnerException property.

HelpLink

The HelpLink property can hold a URL to a help file that provides extensive information about the cause of an exception.

Exceptions Inherited from Exception Class

The .NET framework defines area-related exception classes, which are all derived from the Exception class. The main purpose of these classes is to handle specific type of exceptions. Table 7.1 briefly defines some of these classes. We'll see more detail on these classes in their related articles.

Table-7.11.gif
Table-7.12.gif
Table-7.13.gif

Table7.1: Commonly Used Exceptions

Exception Class Constructors

All derived classes must provide these Exception class constructors:

Exception()
Exception(string)
Exception(string,Exception)

In the last constructor, the argument of type Exception is for the InnerException, which will be explained in detail later in this chapter. The string passed is the error message, which can be obtained through the Message property after construction.

Conclusion


Hope this article would have helped you in understanding Exception Handling in C#. See other articles on the website on .NET and C#.
 


Similar Articles