In this recipe, you will learn how to easily create your own exception types. This is an important lesson to learn as, although C++ exceptions are easy to create yourself, some guidelines should be followed to ensure this is done safely.
Easily creating your own exception classes
Getting ready
Before beginning, please ensure that all of the technical requirements are met, including installing Ubuntu 18.04 or higher and running the following in a Terminal window:
> sudo apt-get install build-essential git cmake
This will ensure your operating system has the proper tools to compile and execute the examples in this recipe. Once this is complete, open a new Terminal. We will use this Terminal to download, compile, and run our examples.
How to do it...
Perform the following steps to try the recipe:
- From a new Terminal, run the following to download the source code:
> cd ~/
> git clone https://github.com/PacktPublishing/Advanced-CPP-CookBook.git
> cd Advanced-CPP-CookBook/chapter02
- To compile the source code, run the following:
> mkdir build && cd build
> cmake ..
> make recipe05_examples
- Once the source code is compiled, you can execute each example in this recipe by running the following commands:
> ./recipe05_example01
The answer is: 42
> ./recipe05_example02
The answer is: 42
> ./recipe05_example03
The answer is: 42
In the next section, we will step through each of these examples and explain what each example program does and how it relates to the lessons being taught in this recipe.
How it works...
Creating your own C++ exceptions allows you to filter out what type of exception you are getting. For example, did the exception come from your code or the C++ library? By creating your own C++ exceptions, you can easily answer these questions during runtime in your own code. Let's look at the following example:
#include <iostream>
#include <stdexcept>
class the_answer : public std::exception
{
public:
the_answer() = default;
const char *what() const noexcept
{
return "The answer is: 42";
}
};
int main(void)
{
try {
throw the_answer{};
}
catch (const std::exception &e) {
std::cout << e.what() << '\n';
}
}
As shown in the preceding example, we create our own C++ exception by inheriting std::exception. This is not a requirement. Technically, anything can be a C++ exception including an integer. Starting from std::exception, however, gives you a standard interface to work from including overriding the what() function, which describes the exception that was thrown.
In this preceding example, we return a hardcoded string in the what() function. This is the ideal type of exception (even more so than the exceptions that are provided by the C++ library). This is because this type of exception is nothrow copy-constructable. Specifically, this means that the exception itself can be copied without the copy generating an exception, for example, due to std::bad_alloc. The exception types provided by the C++ library support construction from std::string(), which could throw std::bad_alloc.
The issue with the preceding C++ exception is that you would need 1 exception type for every type of message you wish to provide. Another way to implement a safe exception type is to use the following:
#include <iostream>
#include <stdexcept>
class the_answer : public std::exception
{
const char *m_str;
public:
the_answer(const char *str):
m_str{str}
{ }
const char *what() const noexcept
{
return m_str;
}
};
int main(void)
{
try {
throw the_answer("42");
}
catch (const std::exception &e) {
std::cout << "The answer is: " << e.what() << '\n';
}
}
In the preceding example, we store a pointer to const char* (that is, a C-style string). C-style strings are stored globally as constants within the program. This type of exception satisfies all of the same preceding rules, and no allocations are taking place during the construction of the exception. It should also be noted that, since the strings are stored globally, this type of operation is safe.
Many types of exceptions can be created using this approach, including things other than strings that are accessible through custom getters (that is, without having to use the what() function). If, however, these preceding rules are not an issue for you, the easiest way to create a custom C++ exception is to simply subclass an existing C++ exception such as std::runtime_error(), as in the following example:
#include <iostream>
#include <stdexcept>
#include <string.h>
class the_answer : public std::runtime_error
{
public:
explicit the_answer(const char *str) :
std::runtime_error{str}
{ }
};
int main(void)
{
try {
throw the_answer("42");
}
catch (const the_answer &e) {
std::cout << "The answer is: " << e.what() << '\n';
}
catch (const std::exception &e) {
std::cout << "unknown exception: " << e.what() << '\n';
}
}
When this example is executed, we get the following:
In the preceding example, we create our own C++ exception in just a few lines of code by subclassing std::runtime_error(). We can then use different catch blocks to figure out what type of exception was thrown. Just remember that if you use the std::string version of std::runtime_error(), you could end up with std::bad_alloc being thrown during the construction of the exception itself.