Exceptions in C#

Imagine you are building a JSON parsing library in C# and the program crashes. You don’t want the user end up having no JSON file parsed and not knowing, what went wrong.

Confused
Figure 1: source

You are not that kind of a developer, are you?

Developer
Figure 2: source
So what are exceptions?

Exception is an event, that stops the execution of the program, due to some behavior, that you, as the programmer, cannot expect. In C#, all types of exceptions derive from the base Exception class in the System namespace.

Consider the following Console Application.

This for loop works well until the point, where the index reaches 4. At this point in time, the index would point to the 5th element in the array (since arrays start at index 0). But there are only 4 elements! This causes the .NET runtime to throw an IndexOutOfRangeException. When debugging the code in Visual Studio, this particular exception looks like this.

You’ve probably already met numerous third-party libraries (either as a developer or end-user), that use custom exceptions, but you never noticed. That is because there was no invalid operation to the program, which would cause the exception to be thrown. If there was and you would want the user to be informed pleasantly, you would have to use .NET exception handling.

.NET exception handling

In C#, the three most used keywords that enable this techique are  throw , try and  catch . They allow you to prevent an exception from completely stopping the program execution and leaving the user wondering, what happened.

For an unexpected behavior to be properly handled, it has to be caught (therefore the catch keyword).

To demonstrate this behavior, I will expand on the previous example. Consider this code.

The whole little “program” we made is inside the try block. Each time the for loop is successfully looped through, the number is written to the console. Then when the program gets to the critical index (see the example application description from So what are exceptions? paragraph for explanation), the catch block is executed. In this example, it would only get executed, when there was an IndexOutOfRangeException thrown. If there was a larger piece of code in the try block and it threw a FileNotFoundException, the program would crash just as any other program without exception handling.

Defining custom exceptions

When you are designing your own CLI compliant library, you can define you own set of exceptions, that best describe what went wrong.

When defining your own exceptions, they have to be placed into a class that inherits from System.Exception (this base class defines three constructors – a default constructor and two custom ones). They can all be created in your custom exception class, but have to call the respective base constructor.

Note: Exception classes should define as many constructors as possible.

An example would look like this.

When throwing CustomException somewhere in the code, you would do  throw new CustomException("Something doesn't work"); .

Leave a Reply

Your email address will not be published. Required fields are marked *