Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Exception Handling

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.

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 header, which we can use in our programs. They are organized in a parent-child class hierarchy, as shown below:

C++ Exception Hierarchy

The following table describes each exception that appears in the hierarchy above:

exception Description
std::exception This exception is the parent class of all standard C++ exceptions.
std::bad_alloc This exception can be thrown by new bad_alloc.
std::bad_cast This exception can be thrown by dynamic_cast bad_alloc.
std::bad_typeid This exception can be thrown by typeid bad_alloc.
std::bad_exception This is very useful when handling unexpected exceptions in a C++ program.
std::logic_error Exceptions that can theoretically be detected by reading the code.
std::domain_error This exception is thrown when an invalid mathematical domain is used.
std::invalid_argument This exception is thrown when an invalid argument is used.
std::length_error This exception is thrown when a std::string that is too long is created.
std::out_of_range This exception can be thrown by methods such as std::vector and std::bitset<>::operator.
std::runtime_error exceptions that theoretically cannot be detected by reading the code.
std::overflow_error This exception is thrown when a mathematical overflow occurs.
std::range_error This exception is thrown when attempting to store a value out of range.
std::underflow_error This exception is thrown when a mathematical underflow occurs.

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.