Exception Types
Gain a thorough understanding of exception types, including how to throw and catch both standard library and custom exceptions in your code
Rather than throwing simple primitive objects like integers and strings, our exceptions get much more powerful when we throw types that are specifically designed to represent errors.
This gives us the ability to define variables and class functions on our error objects, giving our throw
blocks many more options to inspect and react to the exception.
Standard Library Exception Types
The C++ standard library comes with a hierarchy of exception classes. The standard library exception types inherit from std::exception
, but we'll typically be using more specific derived types. Derived types include classes like std::logic_error
and std::runtime_error
.
These have yet more specific subclasses - for example, std::logic_error
has a subclass called std::invalid_argument
These give us a standardized type of object to throw
within our functions. How we classify our exceptions generally doesn't matter, but it's helpful to be consistent.
Throwing and Catching std::exception
objects
The standard library exceptions are available after including <stdexcept>
:
#include <stdexcept>
Throwing exception objects works in the same way as throwing any other object. The standard library types (aside from the basic std::exception
type) have a constructor that we can pass a string to, explaining the issue:
#include <iostream>
#include <stdexcept>
int Divide(int x, int y) {
if (y == 0) {
throw std::invalid_argument {
"Cannot divide by zero"
};
}
return x/y;
}
Similarly, we catch
them like any other. Objects that inherit from std::exception
have a what()
method that allows us to access a description of the exception:
#include <iostream>
#include <stdexcept>
int Divide(int x, int y) {
if (y == 0) {
throw std::invalid_argument {
"Cannot divide by zero"
};
}
return x/y;
}
int main() {
try {
Divide(4,0);
} catch (std::invalid_argument& e) {
std::cout << "Invalid Argument: "
<< e.what();
}
}
Invalid Argument: Cannot divide by zero
Catching By Reference
In the above examples, note that we catch the errors by reference, denoted by the inclusion of the &
character:
catch (std::invalid_argument& e)
By convention, we should generally be catching errors by reference, and by const
reference if preferred.
This is important as, just like functions arguments, exceptions will be passed to catch
blocks by value otherwise. This has twe undesirable effects.
- It causes the usual performance degradation involved in copying an object unnecessarily.
- When dealing with inheritance, as it can result in data loss. If we throw a derived type like
std::invalid_argument
but catch it by the value of a base type likestd::exception
, we lose any class variables that are not part of the basestd::exception
type.
This data loss is sometimes referred to as object slicing - any variables from the more specific types are sliced off when we copy the object to a base type that doesn't contain those variables.
We have an example of this below. Note the two try-catch blocks are almost identical - the only difference is the first block is catching by reference, whilst the second is catching by value:
#include <iostream>
#include <stdexcept>
int Divide(int x, int y) {
if (y == 0) {
throw std::invalid_argument {
"Cannot divide by zero"
};
}
return x/y;
}
int main() {
try {
Divide(2, 0);
} catch (std::exception& e) {
std::cout << "Error 1: " << e.what();
}
try {
Divide(2, 0);
} catch (std::exception e) {
std::cout << "\nError 2: " << e.what();
}
}
Because the second code is copying a std::invalid_argument
into the simpler std::exception
, it has lost all the data that is not part of the std::exception
type.
In most compilers, this will include the custom error message we set, so the second catch
block cannot determine the nature of the error:
Error 1: Cannot divide by zero
Error 2: std::exception
When Multiple Catchers Match
Given the nature of inheritance, multiple catch
statements may match the specific type of exception thrown. When this happens, the first valid catch statement is what is activated. Consider this example:
int main() {
try {
throw std::logic_error("Oops!");
} catch (std::exception& e) {
std::cout << "Exception: " << e.what();
} catch (std::logic_error& e) {
std::cout << "Logic Error: " << e.what();
}
}
Here, we are throwing a std::logic_error
exception. However, because std::logic_error
is a subclass of std::exception
, what we are throwing is also a std::exception
.
As a result, the first catch
statement can handle the error. Our output is:
Exception: std::exception
In this case, the catcher for std::logic_error
is a useless piece of code. It could never be reached because, under the rules of inheritance, if an object is a std::logic_error
it is also a std::exception
,
So, std::logic_error
exceptions will always be handled by the first catch
block. Our tools may warn us as such:
main.cpp:7:12: warning: exception of type 'std::logic_error'
will be caught by earlier handler
Because of this "first catcher wins" behavior, we should ensure the more specific catchers are first:
int main() {
try {
throw std::logic_error("Oops!");
} catch (std::logic_error& e) {
std::cout << "Logic Error: " << e.what();
} catch (std::exception& e) {
std::cout << "Exception:" << e.what();
}
}
Logic Error: Oops!
Creating Custom Exception Types
We are not restricted to just throwing the std::exception
types - we can throw
and catch
any object type.
This allows us to add fields to our errors that are specific to the needs of our application. Below, we introduce a user-defined AuthenticationError
type:
#include <iostream>
class AuthenticationError {
public:
AuthenticationError(
std::string Email, std::string Password)
: Email(Email), Password(Password) {}
std::string Message{"A user failed to log in"};
std::string Email;
std::string Password;
};
int main() {
try {
throw AuthenticationError {
"test@email.com", "something-wrong"
};
} catch (AuthenticationError& e) {
std::cout << e.Message
<< "\n E-Mail: " << e.Email
<< "\n Password: " << e.Password;
}
}
A user failed to log in
E-Mail: test@email.com
Password: something-wrong
Typically, when creating custom exception types, we will want to maintain a hierarchy, similar to std::exception
. This allows us to create more generic catch
statements, by having them catch the more generic base types.
It's also common for teams just to use the standard library's implementation for this purpose, and to insert their types into the std::exception
hierarchy.
When doing that, we should adopt the convention of making our error message available through the what()
function, which we can override:
#include <string>
class AuthenticationError
: public std::exception {
public:
AuthenticationError(
std::string Email, std::string Password)
: Email(Email), Password(Password) {}
const char* what() const noexcept override {
return m_errorMessage.c_str();
};
std::string Email;
std::string Password;
std::string m_errorMessage{
"A user failed to log in"};
};
Now, our exceptions can be caught by specific catch
statements, tailored to handle our specific type:
#include <iostream>
class AuthenticationError{/*...*/}
int main() {
try {
throw AuthenticationError{
"test@email.com", "wrong"};
} catch (AuthenticationError& e) {
std::cout
<< "AuthenticationError Handler:\n"
<< e.what()
<< "\n EMail: " << e.Email
<< "\n Password: " << e.Password;
}
}
AuthenticationError handler:
A user failed to log in
EMail: test@email.com
Password: wrong
But they can also be caught by higher-level, more general handlers:
#include <iostream>
class AuthenticationError{/*...*/}
int main() {
try {
throw AuthenticationError{
"test@email.com", "wrong"};
} catch (std::exception& e) {
std::cout
<< "std::exception Handler:\n"
<< e.what();
}
}
std::exception Handler:
A user failed to log in
Summary
In this lesson, we introduced the notion of exception types, We covered std::exception
and its hierarchy, alongside the creation and handling of custom exception types.
Key Takeaways
- Understanding the standard library has an exception hierarchy, based on
std::exception
and with derived types likestd::logic_error
andstd::runtime_error
. - Techniques for throwing and catching exceptions, emphasizing the importance of catching by reference to avoid object slicing.
- The concept of inheritance in exception handling, ensuring that more specific exceptions are caught before more general ones.
- Best practices for creating and using custom exception types, integrating them into the
std::exception
hierarchy, and overriding thewhat()
method. - The application of the
noexcept
specifier in exception handling, indicating functions that won't throw exceptions.
Using std::terminate()
and the noexcept
Specifier
This lesson explores the std::terminate()
function and noexcept
specifier, with particular focus on their interactions with move semantics.