Provide a utility to throw standard exceptions.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a utility to throw standard exceptions.
-
- Classes:
-
- Canonical Header:
- bsl_stdexcept.h
- See also:
- stdexcept
-
- Description:
- This component provides a means to throw standard exceptions without introducing a compile-time dependency on the standard exception classes. This valuable where header files define function templates or inline functions that may throw these types as exceptions.
-
- Usage:
- First we declare a function template that wants to throw a standard exception. Note that the
stdexcept
header is not included at this point. However, if client code wishes to catch the exception, the .cpp
file must #include
the appropriate header. #include <stdexcept>
void callTestFunction()
{
try {
testFunction<int>(1);
assert(0 == "Should throw before reaching here.");
}
catch(const runtime_error& ex) {
assert(0 == std::strcmp(ex.what(), "sample message 1"));
}
try {
testFunction<double>(2);
assert(0 == "Should throw before reaching here.");
}
catch(const logic_error& ex) {
assert(0 == std::strcmp(ex.what(), "sample message 2"));
}
}
Typedef Documentation