Contents

Catching exceptions

Many programming languages have a concept of exceptions, which are used to disrupt normal program flow when an error is encountered (e.g., trying to access the third element of a two-element array) and go hand in hand with a construct to gracefully handle such exceptions. For example, we can let the user know what happened, prevent a potential segmentation fault, save unsaved data, etc. There are many reasons why handling errors programmatically, rather than letting the program crash.

To disrupt the flow of a program, we say that we throw an exception. The construct in C++ to handle exceptions that are thrown is called a try-catch block. We can raise our own exceptions, which we talk about in the next section, but first we'll find out how to handle exceptions.

The try-catch block looks something like this:

The type will usually be exception (that's the type of exceptions thrown by the standard library), but can be anything (e.g., string, int, or an arbitrary class). Exceptions of type exception always have a method called what(), which returns a string. This is used to describe the exception that occurred. The exception will be the type we will use most often. Here's an example of how we can use it to figure out what happened in a realistic situation:

When this is run, we get:

Caught exception: basic_string

(Or something like this; different compilers will give slightly different information.) Two more tidbits: we can catch exceptions of all types by using the syntax catch(...), and can specify more than one catch, allowing us to handle different types of exceptions differently. Here's an example of one that handles exceptions of type exception, string, and everything else:

(Back to top)

Throwing exceptions

Lets look at throwing exceptions. To throw (or raise) an exception, use the throw keyword. While you can throw any type, lets look at throwing runtime_error exceptions. Consider the following&mdas;be sure to put #include <stdexcept> at the top of your source file:

That should produce the following output:

Caught exception: hey!

You can create your own custom exceptions by subclassing from exception. This article provides some examples of how to do that.

(Back to top)

An example

Lets look at an example of where throwing an error make sense. Lets take the example we used from the section on templates in C++, where we made a wrapper for arrays that keeps track of the size of the array. It would be nice if our Array class could also handle things like out-of-bounds errors. There are two times in our current implementation where that makes sense: getting and setting the value at a given index. The index that's passed in is of type int, so it can be any number within the range of an int, negative or positive. However, our data array is of length size, and as such, the only valid indexes are those between 0 and size-1. So, lets update our method definitions to throw a runtime_exception:

The code that uses the Array class can either catch these exceptions or not. If not, then the exceptions will cause the program to crash. Depending on the compiler, the exception message may be displayed upon crashing. Regardless, the important thing is that we prevented a silent error from propagating, namely the access of out-of-bound regions, which could cause very complex and hard to debug complications later on.

(Back to top)