Exception class:
1. <stdexcept> header file
std::logic_error: Base class for program logic errors.
- std::domain_error: Thrown when the input parameters of a mathematical function exceed the function's domain.
- std::invalid_argument: Thrown when an invalid argument is passed.
- std::length_error: Thrown when trying to create an object that exceeds the maximum allowed size.
- std::out_of_range: Thrown when trying to access an element that does not exist in the container.
std::runtime_error: Base class for runtime errors.
- std::overflow_error: Thrown when a mathematical operation overflows.
- std::underflow_error: Thrown when a mathematical operation underflows.
- std::range_error: Thrown when the range of the calculation result cannot be represented.
2. <new> header file
- std::bad_alloc: Thrown when dynamic memory allocation fails.
- std::bad_array_new_length: Thrown if the length is invalid or too large when trying to allocate an array.
3. <typeinfo> header file
- std::bad_cast: Thrown when `dynamic_cast` fails.
- std::bad_typeid: Thrown when using `typeid` on a `nullptr`.
4. <functional> header file
- std::bad_function_call**: Thrown when calling an empty `std::function` object.
5. <memory> header file
- std::bad_weak_ptr: When constructing `std::shared_ptr`, throw if the passed `std::weak_ptr` is invalid.
6. <future> header file
- std::future_error: An error that occurs when handling a `std::future` or `std::promise` object.
7. <regex> header file
- std::regex_error: An error occurred while processing a regular expression object.
8. <system_error> header file
- std::system_error: Used to report system-level error conditions.
9. <variant> header file
- std::bad_variant_access: Thrown when accessing a `std::variant` object of the wrong type.
10. <optional> header file
- std::bad_optional_access: Thrown when accessing an empty `std::optional` object.
11. <exception> header file
- std::bad_exception: thrown when the exception handler encounters an unpredictable exception.
- std::exception: Base class for all standard exceptions.
12. Concurrency, synchronization and thread-related exceptions
May appear in `<thread>`, `<mutex>`, `<condition_variable>`, `<shared_mutex>` and other header files. These exceptions are mainly related to thread creation, synchronization mechanism, condition variables, etc.
- std::system_error**: Usually used to report thread-related errors, such as thread creation failure.
Each exception class provides a `what()` member function, which returns a string describing the exception. When writing code, it is common to place code that may throw exceptions in a `try` block, and then handle the exception in a `catch` block.
Write your own exception class:
To create your own exception class, you usually inherit from the `std::exception` class or its derived classes and override the `what()` function. The following is a simple example of creating a custom exception class.
Example 1: Direct inheritance from `std::exception`
#include <iostream>
#include <exception>
#include <string>
class MyException : public std::exception {
private:
std::string message;
public:
MyException(const std::string& msg) : message(msg) {}
// Override the what() function
const char* what() const noexcept override {
return message.c_str();
}
};
int main() {
try {
throw MyException("This is my custom exception!");
} catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
return 0;
}
Example 2: Derived from a concrete exception class
#include <iostream>
#include <stdexcept>
#include <string>
class MyOutOfRange : public std::out_of_range {
public:
MyOutOfRange(const std::string& msg) : std::out_of_range(msg) {}
};
int main() {
try {
throw MyOutOfRange("Index is out of range!");
} catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
return 0;
}
In the examples above, the first example shows how to derive a custom exception class directly from `std::exception`, while the second example shows how to derive a custom exception class from `std::out_of_range`, a more specific exception class derived.
In actual use, you can choose the most appropriate base class to derive your own exception class as needed.