C++ Exception Handling
An exception is a problem that arises during program execution. A C++ exception refers to a special situation that occurs at runtime, such as an attempt to divide by zero.
Exceptions provide a way to transfer program control. C++ exception handling involves three keywords:try, catch, throw.
- throw: When a problem occurs, the program throws an exception. This is done by using the throw throw keyword.
- catch: Where you want to handle the problem, the exception is caught by an exception handler.catch The catch keyword is used to catch exceptions.
- try: try The code in the try block identifies the specific exception that will be activated. It is usually followed by one or more catch blocks.
If a try block throws an exception, the method to catch it uses the try and catch try keyword. The try block holds code that may throw an exception; code in a try block is called protected code. The syntax of using a try/catch statement is as follows:
try
{
// Protected code
}
catch
(
ExceptionName
e1
)
{
// catch block
}
catch
(
ExceptionName
e2
)
{
// catch block
}
catch
(
ExceptionName
eN
)
{
// catch block
}
If both the try block throws different exceptions in different situations, you can try to list multiple catch catch statements to catch different types of exceptions.
Throwing an Exception
You can use the throw The throw statement throws an exception anywhere in a code block. The operand of a throw statement can be any expression, and the type of the expression's result determines the type of the thrown exception.
The following is an example of throwing an exception when attempting to divide by zero:
double
division
(
int
a
,
int
b
)
{
if
(
b
==
0
)
{
throw
"
Division by zero condition!
"
;
}
return
(
a
/
b
)
;
}
Catching an Exception
catch A catch block follows a try try block and is used to catch exceptions. You can specify the type of exception you want to catch, which is determined by the exception declaration in parentheses after the catch keyword.
try
{
// Protected code
}
catch
(
ExceptionName
e
)
{
// Code to handle the ExceptionName exception
}
The code above will catch an exception of type ExceptionName exception. If you want the catch block to handle any type of exception thrown by the try block, you must use an ellipsis ... inside the exception declaration parentheses, as shown below:
try
{
// Protected code
}
catch
(
...
)
{
// Code that can handle any exception
}
Below is an example that throws a divide-by-zero exception and catches it in a catch block.
Example
#include
<
iostream
using
namespace
std;
double
division
(
int
a
,
int
b
)
{
if
(
b
==
0
)
{
throw
"
Division by zero condition!
"
;
}
return
(
a
/
b
)
;
}
int
main
(
)
{
int
x
=
50
;
int
y
=
0
;
double
z
=
0
;
try
{
z
=
division
(
x
,
y
)
;
cout
<<
z
<<
endl
;
}
catch
(
const
char
msg
)
{
cerr
<<
msg
<<
endl
;
}
return
0
;
}
Since we threw an exception of type const charconst char*, when catching it we must use const char* in the catch block. When the code above is compiled and executed, it produces the following result:
Division by zero condition!
Standard Exceptions in C++
C++ provides a series of standard exceptions, defined in the

The following table describes each exception that appears in the hierarchy above:
Defining New Exceptions
You can define new exceptions by inheriting from and overloading the exception std::exception class. The example below demonstrates how to implement your own exception using the std::exception class:
Example
#include
<
iostream
#include
<
exception
using
namespace
std;
struct
MyException
:
public
exception
{
const
char
what
(
)
const
throw
(
)
{
return
"
C++ Exception
"
;
}
}
;
int
main
(
)
{
try
{
throw
MyException
(
)
;
}
catch
(
MyException
&
e
)
{
std
::
cout
<<
"
MyException caught
"
<<
std
::
endl
;
std
::
cout
<<
e
.
what
(
)
<<
std
::
endl
;
}
catch
(
std
::
exception
&
e
)
{
// Other errors
}
}
This will produce the following result:
MyException caught
C++ Exception
Here,what() is a public method provided by the exception class; it has been overloaded by all child exception classes. It returns the cause of the exception.