Pluralsight Logo
Author avatar

Peter Mbanguo

Author badge Author

Understanding the System.Exception Class

Peter Mbanguo

Author BadgeAuthor
  • Oct 16, 2018
  • 5 Min read
  • 18 Views
  • Oct 16, 2018
  • 5 Min read
  • 18 Views
C#

Introduction

Writing software is a complex undertaking, and it is quite common for even the best software to ship with various problems. Sometimes the problem is caused by bad code, other times, a problem is caused by bad user input that has not been accounted for in the application’s code. Regardless of the cause of the problem, the end result is that the application does not work as expected. At this point, we say the application has encountered an error. In .NET, exceptions are thrown when a running application encounters an error.

What Are Exceptions?

An exception is a runtime error in a program that violates a system or application constraint, or a condition that is not expected to occur during normal execution of the program. Possible exceptions include attempting to connect to a database that no longer exists, when a program tries to divide a number by zero, or opening a corrupted XML file. When these occur, the system catches the error and raises an exception.

The Exception Class Hierarchy

In .NET, an exception accounts for bugs, bad user input, and runtime errors, even though programmers may view each of these as distinct issues. They're represented by object instances. We have different exception classes representing different types of errors. All exception types inherit from the System.Exception base class.

The SystemException class inherits from the Exception base class. The OutOfMemoryException, StackOverflowException, and ArgumentException classes inherit from SystemException. The ArgumentException class has two other classes which derive from it; the, ArgumentNullException and ArgumentOutOfRangeException classes.

The ArithmeticException class derives from the Exception base class. The OverflowException and DivideByZero exceptions then inherit from the ArithmeticException class.

We also have the ApplicationException class which is derived from Exception base class. Additionally, we can define our own exception classes and they can derive from the Exception base class.

The System.Exception Class

All exceptions ultimately derive from the System.Exception base class, which in turn derives from System.Object. It has a couple of constructors and properties. The most important properties of System.Exception are as follows:

  1. StackTrace: This is a string representing all the methods that are called from the origin of the exception to the catch block. It shows the path or flow that lead to the exception. This is very useful when debugging.

  2. Message: This is a string that describes the error that occurred. This description should completely describe the error and, if possible, where and how to fix it.

  3. InnerException: This property is of System.Exception type, and captures the preceding exception which led to the current exception. This way of capturing and wrapping preceding exceptions is known as exception wrapping. If there isn't any exception leading to this, it is null.

Other Properties of the Exception Class Are:

  1. Data: This property is of type IDictionary, which gives us key/value pair. The key is of type string and the value is of the object type. This property can help us supply additional exception data.

  2. Source: This property is of type string and it describes the application or object that caused the error. By default, this is set to the name of the originating assembly.

  3. HResult: This property is of type int and represents an HResult value, often used with COM interop code.

  4. HelpLink: This property is of type string and it specifies a link to an associated help file for the error. It may be in the form of a Uniform Resource Locator (URL), or Uniform Resource Name (URN).

  5. TargetSite: This property is of type System.Reflection.MethodBase and it gives us access to the method that threw the exception.

Commonly Used System.Exception Constructor

There's a couple of constructors available on the Exception class. The simplest constructor doesn't have any parameters and, along with it, we get a default Message property with a null InnerException property.

1
var exception = new Exception();

There's a constructor overload that takes a message parameter, which maps to the Message property. The InnerException property is also null here.

1
var exception = new Exception("error message");

There's also a constructor overload that takes a message parameter, which maps to the Message property, and an innerException parameter, which maps to the InnerException property and is useful for exception wrapping.

1
var exception = new Exception("error message", new Exception());

Wrap Up

At some point, an application encounters an error. Sometimes the error is caused by bad code, other times, it is caused by bogus user input that has not been accounted for in the application’s code. In .NET, exceptions are thrown when a running application encounters an error. In this guide, we looked at exception class hierarchy in .Net and went deep into the properties and constructors of the Exception class, from which other exception types in .Net derives from.

2