Multilevel Exceptions and User Defined Exceptions[1]
Multilevel Exceptions and User Defined Exceptions[1]
defined exceptions
Object oriented programming seminar
24/3/2025
Presented By:-
Abhishek.S : RA2411003020934
Salai Kamalavasan.S : RA2411003020943
C.M.Pranith : RA2411003020952
Multilevel exceptions-multiple catch
• It is possible to associate more than one catch statement with a single try block.
• This is usually done when a program segment has more than one condition to
throw as an exception.
• In such cases, when an exception is thrown, the exception handlers are searched
to find an appropriate match. Then, the first matched catch block is executed.
After execution, the program control goes to the first statement after the last catch
block for that try block. This means that all other catch blocks are ignored.
• However, if no match is found, then the program is terminated using the default
abort ().
Syntax for multiple catch:-
try
{ //try block }
catch(data type1 arg)
{ //catch block1 }
……………....
catch(data type2 arg)
{ //catch block2 }
………………
catch(data typeN arg)
{ //catch blockN}
Example program
In some situation, the exception handler in the catch block may decide to rethrow the exception
without processing it.
If a catch block cannot handle the particular exception it has caught, it can rethrow the
exception.
Syntax to rethrow the exception is throw;
A throw statement without any exception explicitly mentioned causes the current exception to be
thrown to the next try catch block.
Example:-
catch (char e) {
cout << "Caught in test() - Character: "
<< e << endl;
throw; // Rethrow the exception
Restricting the Exceptions that can be Thrown
To restrict a function to throw only certain specified exceptions, throw list clause is used in the
function defintion.
Syntax:
return_type function_name(arg-list) throw(type-list)
{
// ...
}
Handling uncaught exception
Example Program:-