0% found this document useful (0 votes)
2 views21 pages

Exception Handling

The document provides an overview of exception handling in C++, detailing the typical development approach and the importance of preparing for exceptional cases. It explains the try-throw-catch mechanism, types of exceptions (synchronous and asynchronous), and standard exceptions in C++. Examples illustrate how to implement exception handling in C++ programs.

Uploaded by

aa.raxmanovv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Exception Handling

The document provides an overview of exception handling in C++, detailing the typical development approach and the importance of preparing for exceptional cases. It explains the try-throw-catch mechanism, types of exceptions (synchronous and asynchronous), and standard exceptions in C++. Examples illustrate how to implement exception handling in C++ programs.

Uploaded by

aa.raxmanovv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Exception Handling

Introduction

 Typical approach to development:


 Write programs assuming things go as planned
 Get "core" working
 Then take care of "exceptional" cases
 C++ exception-handling facilities
 Handle "exceptional" situations
 Mechanism "signals" unusual happening
 Another place in code "deals" with exception
Introduction to Exception
Handling
 Sometimes the best outcome can be
when nothing unusual happens
 However, the case where exceptional
things happen must also be prepared for
C++ exception handling facilities are
used when the invocation of a method
may cause something exceptional to
occur
Introduction to Exception
Handling
 C++ library software (or programmer-defined
code) provides a mechanism that signals when
something unusual happens
 This is called throwing an exception
 In another place in the program, the programmer
must provide code that deals with the
exceptional case
 This is called handling the exception
Types of C++ Exception
There are two types of exceptions in C++:
1. Synchronous: Exceptions that happen when
something goes wrong because of a mistake in the
input data or when the program is not equipped to
handle the current type of data it’s working with,
such as dividing a number by zero.
2. Asynchronous: Exceptions that are beyond the
program’s control, such as disc failure, keyboard
interrupts, etc.
try-throw-catch Mechanism
Syntax of try-catch in C++
try-throw-catch Mechanism
1. The try keyword represents a block of code that may
throw an exception placed inside the try block.
2. The catch statement represents a block of code that is
executed when a particular exception is thrown from the
try block.
3. When a program encounters a throw statement, then it
immediately terminates the current function and starts
finding a matching catch block to handle the thrown
exception.
Example 1
int main()
{
// try block
try {
int numerator = 10;
int denominator = 0;
int res;
// check if denominator is 0 then throw runtime
// error.
if (denominator == 0) {
throw runtime_error(
"Division by zero not allowed!");
}
// calculate result if no exception occurs
res = numerator / denominator;
//[printing result after division
cout << "Result after division: " << res << endl;
}
// catch block to catch the thrown exception
catch (const exception& e) {
// print the exception
cout << "Exception " << e.what() << endl;
}
return 0;
}
Example 2
int main()
{
int x = -1;
// Some code
cout << "Before try \n";
// try block
try {
cout << "Inside try \n";
if (x < 0) {
// throwing an exception
throw x;
cout << "After throw (Never executed) \n";
}
}
// catch block
catch (int x) {
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
return 0;
}
try-throw-catch Mechanism

 When a try block is executed, three


things can happen:
1. No exception is thrown in the try block
– The code in the try block is executed to the
end of the block
– The catch block is skipped
– The execution continues with the code placed
after the catch block
try-throw-catch Mechanism

2. An exception is thrown in the try block


and caught in the catch block
– The rest of the code in the try block is skipped
– Control is transferred to a following catch
block
– The thrown object is plugged in for the catch
block parameter
– The code in the catch block is executed
– The code that follows that catch block is
executed (if any)
try-throw-catch Mechanism

3. An exception is thrown in the try block and there


is no corresponding catch block to handle the
exception
– The rest of the code in the try block is skipped
– The function throws the exception to its calling
function.
– The calling function either catches the exception
using a catch block, or itself throws the
exception.
try block
 Basic method of exception-handling is
try-throw-catch
 Try block:

try
{
Some_Statements
<some code with a throws statement or a function
invocation that might throw an exception>
Some_More_Statements
}

 Contains code for basic algorithm when all


goes smoothly
 A try block must be followed by at least one catch block
throw
 Inside try-block, when something unusual happens:
try
{
Code_To_Try
if (exceptional_happened)
throw object;
More_Code
}

 Keyword throw followed by an object id


 Called "throwing an exception“
 An exception can be thrown outside of a try block.
catch-block
 When something thrown → goes somewhere
 In C++, flow of control goes from try-block to
catch-block
 try-block is "exited" and control passes to catch-block
 Executing catch block called "catching the
exception"
 Exceptions must be "handled" in some
catch block
 There cannot be a catch block without a
preceding try block.
catch-block
 Recall:

catch(int e)
{
cout << e << " donuts, and no milk!\n";
<< " Go buy some milk.\n";
}
 Looks like function definition with
int parameter!
 Not a function, but works similarly
 Throw like "function call"
catch-block Parameter
 Recall: catch(int e)
 "e" called catch-block parameter
 Each catch block can have at most ONE
catch-block parameter
 Does two things:
1. type name specifies what kind of thrown
value the catch-block can catch
2. Provides name for thrown value caught;
can "do things" with value
Multiple Throws and Catches

 try-block typically throws any number of


exception values, of differing types
 Of course only one exception thrown
 Since throw statement ends try-block
 But different types can be thrown
 Each catch block only catches "one type"
 Typical to place many catch-blocks after each
try-block
C++ Standard Exceptions
std::exception
1
An exception and parent class of all the standard C++ exceptions.

std::bad_alloc
2
This can be thrown by new.
std::bad_cast
3
This can be thrown by dynamic_cast.
std::bad_exception
4 This is useful device to handle unexpected exceptions in a C++
program.
std::bad_typeid
5
This can be thrown by typeid.

std::logic_error
6
An exception that theoretically can be detected by reading the code.
C++ Standard Exceptions
std::domain_error
7 This is an exception thrown when a mathematically invalid domain is
used.
std::invalid_argument
8
This is thrown due to invalid arguments.
std::length_error
9
This is thrown when a too big std::string is created.

std::out_of_range
10 This can be thrown by the 'at' method, for example a std::vector and
std::bitset<>::operator[]().

std::runtime_error
11 An exception that theoretically cannot be detected by reading the
code.

std::overflow_error
12
This is thrown if a mathematical overflow occurs.
Standard Library exception
classes

You might also like