0% found this document useful (0 votes)
15 views10 pages

Multilevel Exceptions and User Defined Exceptions[1]

The document discusses multilevel exceptions and user-defined exceptions in C++, highlighting the use of multiple catch blocks, catch-all clauses, and rethrowing exceptions. It explains the syntax for handling exceptions and the concept of restricting exceptions using throw lists. Additionally, it covers the creation of user-defined exceptions by inheriting from the exception class and contrasts the 'throw' and 'throws' keywords in exception handling.

Uploaded by

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

Multilevel Exceptions and User Defined Exceptions[1]

The document discusses multilevel exceptions and user-defined exceptions in C++, highlighting the use of multiple catch blocks, catch-all clauses, and rethrowing exceptions. It explains the syntax for handling exceptions and the concept of restricting exceptions using throw lists. Additionally, it covers the creation of user-defined exceptions by inheriting from the exception class and contrasts the 'throw' and 'throws' keywords in exception handling.

Uploaded by

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

Multilevel exceptions and User

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

#include <iostream> //multiple catches //main function


catch (int x) int main()
using namespace std; {
{ cout << "Catch a integer and that integer is
void test(int x) { positive:" << x<<endl; } cout << "Testing multiple catches\n";
test(10);
try test(-100);
catch (char x)
{ { cout << "Catch a integer and that integer is test(0);
negative:" << x<<endl; } return 0;
if (x > 0) }
throw x; catch (const char* x)
else if (x < 0) { cout << "Catch a integer and that integer Output
is:" << x; }
throw 'N'; } Testing multiple catches
Catch a integer and that integer is positive:10
else
Catch a integer and that integer is negative:N
throw "zero"; } Catch a integer and that integer is:zero
Catch All Exceptions or Default Exception Handling

 The catch-all clause, catch (...) matches exceptions of any type.


 If present, it has to be the last catch clause in the handler-seq.
 Catch-all block may be used to catch every possible exception thrown from the try block.

catch (...) { // Catch-all block


cout << "Caught an unknown exception"
<< endl;
}
Rethrowing Exception:

 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

 Also known as stack unwinding


 A call stack, function stack, execution stack, run time stack, or a control stack is used to keep a
track point to which the active function will return after completing its execution.
 If a Function doesn’t handle the exception, the program stack unwinds the stack and control
returns to main(). But there’s no exception handler here either in main, then main() terminates. At
this point, we just terminated our application
User Defined Exceptions
 C++ allows programmers to define their own exceptions by inheriting and overriding exception class
functionality.

Example Program:-

#include <iostream> //Main function


#include <exception> int main() {
using namespace std; try {
// inherting class exception throw MyException(); //throwing customized exception
struct MyException : public exception }
catch(MyException & e)
{
{ cout << "MyException caught" << endl;
const char * what () const throw () cout << e.what() << endl;
{ return "C++ Exception"; }
} cout<<"\n Existing main()"; return 0;
}; }
throw and throws

S.N throw throws


o
1. throw keyword is used to explicitly The throws keyword is used to declare
throw an exception within a method which exceptions can be thrown from a
or block of code method

2. Internally throw is implemented as it On other hand we can declare multiple


is allowed to throw only single exceptions with throws keyword that could
exception at a time i.e we cannot get thrown by the function where throws
throw multiple exception with throw keyword is used
keyword.
3. Supported by C++ Not supported by C++

• Also C++ does not support ‘finally’


blocks.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy