C++ Exception Handling Library
is a header file in the C++ standard library that defines a set of standard exception classes for handling runtime error conditions.
Exceptions are errors that occur during program execution; they can be caught and handled to avoid abnormal termination of the program. The header defines a set of exception classes derived from std::exception , which provide a standard way to report and handle errors.
Syntax
To use 's exception classes, you first need to include this header:
#include
Then, you can use these exception classes to throw and catch exceptions. For example:
throw std::runtime_error("An error occurred");
Exception classes
The header 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: Whentypeidoperation fails, it is thrown.std::logic_error: Thrown when a logic error occurs, such as an invalid input parameter.std::domain_error: Thrown when a function's argument is out of the valid range.std::invalid_argument: Thrown when a function's argument is invalid.std::length_error: Thrown when a container operation fails due to a length limit.std::out_of_range: Thrown when accessing an illegal index of a container.std::overflow_error: Thrown when an arithmetic operation causes an overflow.std::range_error: Thrown when a function's return value is outside the expected range.std::underflow_error: Thrown when an arithmetic operation causes an underflow.
Example
Below is an example using simple example, demonstrating how to throw and catch exceptions:
Example
#include
#include
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 result:
Caught an exception: Negative value not allowed
In this example,riskyFunction function checks whether the passed argument is negative; if so, it throws std::invalid_argument exception. In the main In the function, we use try-catch block to catch and handle this exception.
Standard exception classes
std::exception
std::exception is the base class of all standard exception classes. It provides a virtual function what(), used to return a string describing the exception.
Example
#include
#include
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 a program logic error. It is an abstract base class, usually made concrete by derived classes.
Example
#include
#include
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 is derived from std::logic_error and represents an invalid argument being passed.
Example
#include
#include
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 is derived from std::logic_error and represents an argument out of the valid range.
Example
#include
#include
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 a container operation.
Example
#include
#include
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 is derived from std::logic_error and represents access beyond the valid range.
Example
#include
#include
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 is derived from std::exception and represents a program runtime error. It is an abstract base class, usually made concrete by derived classes.
Example
#include
#include
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 is derived from std::runtime_error and represents a computation result out of the representable range.
Example
#include
#include
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 is derived from std::runtime_error and represents an arithmetic overflow error.
Example
#include
#include
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 is derived from std::runtime_error and represents an arithmetic underflow error.
Example
#include
#include
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 a custom exception class
In addition to standard exception classes, we can also define our own exception classes, inheriting from standard exception classes.
Example
#include
#include
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
;
}
is an important part 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 's exception classes is very important.