Lecture 3.1.2 3.1.3 Types and Rethrow
Lecture 3.1.2 3.1.3 Types and Rethrow
Course Objectives
2
Course Outcomes
CO Title Level
Number
4
CONTENTS
• Types of Exceptions
• Throwing an Exception
• Catching an Exception (All types of
throw and catch)
• Rethrow Exception
5
Types of Exceptions
6
Throwing Exceptions
• Exceptions can be thrown anywhere within a code block
using throw statement. The operand of the throw statement determines a
type for the exception and can be any expression and the type of the result
of the expression determines the type of exception thrown.
• Following is an example of throwing an exception when dividing by zero
condition occurs −
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
7
Catching Exceptions
• The catch block following the try block catches any exception. You can specify what type of exception you
want to catch and this is determined by the exception declaration that appears in parentheses following the
keyword catch.
try {
// protected code
} catch( ExceptionName e ) {
// code to handle ExceptionName exception
}
• Above code will catch an exception of ExceptionName type. If you want to specify that a catch block should
handle any type of exception that is thrown in a try block, you must put an ellipsis, ..., between the
parentheses enclosing the exception declaration as follows −
try {
// protected code
} catch(...) {
// code to handle any exception
}
8
MULTIPLE CATCH
• The above program is self-explanatory, if the value of integer in the array x is less than 0, we are throwing a numeric value as exception and if the
value is greater than 0, then we are throwing a character value as exception. And we have two different catch blocks to catch those exceptions.
10
Generalised catch block in C++
• Below program contains a generalized catch block to catch any uncaught errors/exceptions. catch(...) block takes care of all type of
exceptions.
• In the case above, both the exceptions are being catched by a single catch block. We can even have separate catch blocks to handle
integer and character exception along with the generalised catch block.
11
Re-throwing an Exception
• It’s possible that an exception handler, upon receiving an exception, might decide
either that it cannot process that exception or that it can process the exception only
partially.
• In such cases, the exception handler can defer the exception handling (or perhaps
a portion of it) to another exception handler.
• In either case, you achieve this by re throwing the exception via the statement
throw;
• Regardless of whether a handler can process an exception, the handler can re
throw the exception for further processing outside the handler.
26/05/2025 4
RETHROW EXCEPTION
14
Throwing the exception in
Function
15
Re-throwing an Exception
#include <iostream>
#include <exception>
using namespace std;
void func1()
{
// throw exception and catch it immediately
try
{
cout << "\n This function throws an exception\n";
throw exception(); //generate exception
} // end try
catch(exception &)
{
cout << " exception is catched in 1st catch block"
<< "\n Function func1 re throws exception";
throw; //rethrow exception for further processing
} // end catch
cout << "This also should not print\n";
} // end of func1
26/05/2025 5
Re-throwing an Exception
int main()
{
try
{
cout << "\n main invokes function func1\n";
func1();
cout << "This should not print\n";
} // end try
catch (exception e) // handle exception
{
cout << "\n\n Exception handled in main\n";
} // end catch
cout << " Program control continues after catch in main\n";
} // end main
26/05/2025 5
Output:
18
Applications
• Helps in finding and handling run-time anomalies or abnormal
conditions that a program encounters during its
execution.
19
Summary
20
Frequently Asked question
Q1 Describe the way to handle run time error in C++.
Answer: A runtime error is handled in C++ in the following manner:
- Write the code where the error is predicted after the keyword try.
- Write the solution if a particular exception occurs in the catch block by specifying a parameter which is thrown from the try block.
Or
- Throw the error object by using throw(Exception type object) after try keyword.
- It is called whenever the exception handling mechanism cannot find a handler for a thrown exception.
- The unexpected() is called when a function with an exception specification throws an exception of a type that is not listed in the exception
specification for the function
- - A function declaration without a specification like throw(char*) may throw any type of exception, and one with throw() is not allowed to throw
exceptions at all.
22
Assessment Questions:
3. Which part of the try-catch block is always fully executed?
a) try part
b) catch part
c) finally part
d) throw part
23
Discussion forum.
• What are the various synchronous and asynchronous exceptions?
• Examples of various exception handling scenarios.
24
REFERENCES
TEXT BOOKS
T1 E Balagurusamy., “Object Oriented Programming in C++”, Tata McGraw-Hill.
T2 Robert Lafore, “Object Oriented Programming in C++”, Waite Group.
REFERENCE BOOKS
R1 Herbert Schildt , “C++- The Complete Reference”, Tata McGraw-Hill 2003, New
Delhi.
R2 Bjarne Stroustrup: “The C++ Programming Language” (4th Edition). Addison-Wesley.
R3 Ravichandran , “Programming with C++”,Tata McGraw-Hill Education.
R4 Joyce M. Farrell,” Object Oriented Programming Using C++”, Learning.
R5 Programming Languages: Design and Implementation (4th Edition), by Terrence W.
Pratt, Marvin V. Zelkowitz, Pearson.
R6 Programming Language Pragmatics, Third Edition, by Michael L. Scott, Morgan
Kaufmann.
Websites:
1. https://www.sanfoundry.com/cplusplus-programming-questions-answers-exce
ption-handling-1/
2. https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
3. https://www.geeksforgeeks.org/exception-handling-c/ 25
THANK YOU