C++ Exception Handling Library <stdexcept>
<stdexcept>Is a header file in the C++ standard library that defines a set of standard exception classes for handling error conditions during program runtime.
Exceptions are errors that occur during program runtime; they can be caught and handled to prevent abnormal program termination.<stdexcept>The header file defines a set of classes derived fromstd::exceptionDerived exception classes that provide a standard way to report and handle errors.
Syntax
To use<stdexcept>For the exception classes involved, you first need to include this header file:
#include <stdexcept>
Then, you can use these exception classes to throw and catch exceptions. For example:
throw std::runtime_error("An error occurred");
Exception class
<stdexcept>The header file defines the following exception classes:
std::exception: The base class of all standard exception classes.std::bad_exception: Thrown when an error occurs during exception handling.std::bad_alloc: Thrown when memory allocation fails.std::bad_cast: Thrown when type conversion fails.std::bad_typeid: whentypeidThrown when an operation fails.std::logic_error: Thrown when a logic error occurs, such as an invalid input parameter.std::domain_error: Thrown when an argument to a function call is outside the valid range.std::invalid_argument: Thrown when an argument to a function call is invalid.std::length_error: Thrown when a container operation fails because of length constraints.std::out_of_range: Thrown when an illegal index is accessed in a container.std::overflow_error: Thrown when arithmetic operations cause overflow.std::range_error: Thrown when a function return value is outside the expected range.std::underflow_error: Thrown when arithmetic operations cause underflow.
Example
Below is a usage example of<stdexcept>A simple example demonstrating how to throw and catch exceptions:
Example
#include <stdexcept>
void riskyFunction(int x) {
if (x < 0) {
throw std::invalid_argument("Negative value not allowed");
}
std::cout << "Processing " << x << std::endl;
}
int main() {
try {
riskyFunction(-1);
} catch (const std::invalid_argument& e) {
std::cerr << "Caught an exception: " << e.what() << std::endl;
}
return 0;
}
Output:
Caught an exception: Negative value not allowed
In this example,riskyFunctionThe function checks whether the passed parameter is negative; if so, it throwsstd::invalid_argumentException. InmainIn the function, we usetry-catcha block to catch and handle this exception.
Standard exception classes
std::exception
std::exception is the base class for all standard exception classes. It provides a virtual function what() that returns a string describing the exception.
Example
#include <exception>
int main() {
try {
throw std::exception();
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << std::endl;
}
return 0;
}
std::logic_error
std::logic_error is derived from std::exception and represents program logic errors. It is an abstract base class, usually concretized by derived classes.
Example
#include <stdexcept>
int main() {
try {
throw std::logic_error("Logic error occurred");
} catch (const std::logic_error& e) {
std::cout << "Caught logic error: " << e.what() << std::endl;
}
return 0;
}
std::invalid_argument
std::invalid_argument derives from std::logic_error, indicating that an invalid argument was passed.
Example
#include <stdexcept>
int main() {
try {
throw std::invalid_argument("Invalid argument provided");
} catch (const std::invalid_argument& e) {
std::cout << "Caught invalid argument: " << e.what() << std::endl;
}
return 0;
}
std::domain_error
std::domain_error derives from std::logic_error, indicating that an argument is outside the valid domain.
Example
#include <stdexcept>
int main() {
try {
throw std::domain_error("Domain error occurred");
} catch (const std::domain_error& e) {
std::cout << "Caught domain error: " << e.what() << std::endl;
}
return 0;
}
std::length_error
std::length_error is derived from std::logic_error and represents a length error, such as exceeding the maximum length limit in container operations.
Example
#include <stdexcept>
int main() {
try {
throw std::length_error("Length error occurred");
} catch (const std::length_error& e) {
std::cout << "Caught length error: " << e.what() << std::endl;
}
return 0;
}
std::out_of_range
std::out_of_range derives from std::logic_error, indicating that an access is out of range.
Example
#include <stdexcept>
int main() {
try {
throw std::out_of_range("Out of range error occurred");
} catch (const std::out_of_range& e) {
std::cout << "Caught out of range error: " << e.what() << std::endl;
}
return 0;
}
std::runtime_error
std::runtime_error derives from std::exception and represents program runtime errors. It is an abstract base class, usually made concrete by derived classes.
Example
#include <stdexcept>
int main() {
try {
throw std::runtime_error("Runtime error occurred");
} catch (const std::runtime_error& e) {
std::cout << "Caught runtime error: " << e.what() << std::endl;
}
return 0;
}
std::range_error
std::range_error derives from std::runtime_error, indicating that a calculation result exceeds the representable range.
Example
#include <stdexcept>
int main() {
try {
throw std::range_error("Range error occurred");
} catch (const std::range_error& e) {
std::cout << "Caught range error: " << e.what() << std::endl;
}
return 0;
}
std::overflow_error
std::overflow_error derives from std::runtime_error, indicating an arithmetic overflow error.
Example
#include <stdexcept>
int main() {
try {
throw std::overflow_error("Overflow error occurred");
} catch (const std::overflow_error& e) {
std::cout << "Caught overflow error: " << e.what() << std::endl;
}
return 0;
}
std::underflow_error
std::underflow_error derives from std::runtime_error, indicating an arithmetic underflow error.
Example
#include <stdexcept>
int main() {
try {
throw std::underflow_error("Underflow error occurred");
} catch (const std::underflow_error& e) {
std::cout << "Caught underflow error: " << e.what() << std::endl;
}
return 0;
}
Using custom exception classes
Besides standard exception classes, we can also define our own exception classes that inherit from standard exception classes.
Example
#include <stdexcept>
class MyException : public std::runtime_error {
public:
MyException(const std::string& message) : std::runtime_error(message) {}
};
int main() {
try {
throw MyException("My custom exception occurred");
} catch (const MyException& e) {
std::cout << "Caught custom exception: " << e.what() << std::endl;
}
return 0;
}
<stdexcept>It is an important component of the C++ standard library. It provides a set of standard exception classes, making error handling more consistent and predictable. By using these exception classes, you can write more robust and maintainable code. For beginners, understanding the basic concepts of exceptions and how to use them ...<stdexcept>The exception classes in it are very important.