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.
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.
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.
SystemException class inherits from the
Exception base class. The
ArgumentException classes inherit from
ArgumentException class has two other classes which derive from it; the,
ArithmeticException class derives from the
Exception base class. The
DivideByZero exceptions then inherit from the
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.
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:
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.
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.
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.
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.
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.
HResult: This property is of type
int and represents an HResult value, often used with COM interop code.
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).
TargetSite: This property is of type
System.Reflection.MethodBase and it gives us access to the method that threw the exception.
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
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.
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.
var exception = new Exception("error message", new Exception());
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.
Test your skills. Learn something new. Get help. Repeat.Start a FREE 10-day trial