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.