The document discusses exception handling in C++. It describes two types of exceptions: synchronous and asynchronous. It explains how to use try, catch, and throw blocks to handle exceptions. Specifically, it covers throwing and catching different exception types, using multiple catch blocks, and catching all exceptions. It also discusses how to handle uncaught exceptions using terminate() and set_terminate(), as well as unexpected() and set_unexpected() to handle exceptions not specified in the function declaration.
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
13 exception handling
1. 09/04/131 VIT - SCSE
• Logic errors
• Syntactic errors
• Mechanisms for handling run time errors
• 2 types of exceptions
• Synchronous exception
• e.x: errors such as “out-of-range index” and “over-flow”
• Asynchronous exception
• e.x: such as a keyboard interrupts
Exception Handling
2. 09/04/132 VIT - SCSE
The proposed method is only for handling synchronous
exceptions
1.find the problem (hit the exception)
2.inform that an error has occurred (throw the exception)
3.receive the error information (catch the exception)
4.take corrective actions (handle the exception)
3. 09/04/133 VIT - SCSE
try block
Detects and throw an exception
catch block
Catches and handles the exception
11. 09/04/1311 VIT - SCSE
terminate()
•the function terminate() is invoked when an exception is
raised and the handler is not found.
•The default action for terminate is to invoke abort().
•Such a default action causes immediate termination of the
program execution.
12. 09/04/1312 VIT - SCSE
class excep1
{
};
class excep2
{
};
void main()
{
try
{
cout<<”Throwing uncaught exception”<<endl;
throw excep2();
}
catch(excep1) //true if throw excep1 is executed in try scope
{
//action for exception
cout<<”Exception1”;
}
//excep2 is not caught hence, program aborts here without proceeding
further
cout<<”I am not displayed”;
}
13. 09/04/1313 VIT - SCSE
Output:
Throwing uncaught exception
Program aborted
14. 09/04/1314 VIT - SCSE
set_terminate
user can define their own exception handler
or
user defined exception
set_terminate is defined in except.h
15. 09/04/1315 VIT - SCSE
The program to handle uncaught exceptions with the user specified
terminate function
//all exceptions are not caught, executes MyTerminate()
#include<process.h>
#include<except.h>
class excep1
{
};
class excep2
{
};
void MyTerminate()
{
cout<<”My Terminate is invoked”;
exit(1);
}
16. 09/04/1316 VIT - SCSE
void main()
{
set_terminate(MyTerminate); //set to our own terminate function
try
{
cout<<”Throwing uncaught exception”<<endl;
throw excep2();
}
catch(excep1) //true if throw excep1 is executed in try scope
{
//action for exception
cout<<”Caught exception1”;
}
//program abort() here, MyTransaction will be called
cout<<”I am not displayed”;
}
17. 09/04/1317 VIT - SCSE
Output:
Throwing uncaught exception
My Terminate is invoked
18. 09/04/1318 VIT - SCSE
unexpected()
The unexpected () function is called when a function throws an
exception not listed in its exception specification.
The program calls unexpected() which calls any user-defined
function registered by set_unexpected().
If no function is registered with set_unexpected, the
unexpected() function then invokes the terminate() function.
The prototype of the unexpected() call is
void unexpected();
the function unexpected() returns nothing (void).
But it can throw an exception through the execution of a
function registered by the set_unexpected function.
19. 09/04/1319 VIT - SCSE
//unexpected exception
#include<except.h>
class zero
{
};
void dis(int num) throw()
{
if(num>0)
cout<<"+ve number";
else
if(num<0)
cout<<"-ve number";
else
throw zero(); //unspecified exception
}
void main()
{
int num;
cout<<"Enter any
number";
cin>>num;
try
{
dis(num);
}
catch(...)
{
cout<<"catch all
exceptions";
}
cout<<endl<<"end of
main";
getch();
}
20. 09/04/1320 VIT - SCSE
Output 1:
Enter any number 10
+ve number
end of main
Output 2:
Enter any number -5
-ve number
end of main
Output 3:
Enter any number 0
Program aborted
21. 09/04/1321 VIT - SCSE
set_unexcepted()
#include<process.h>
//has prototype for exit()
#include<except.h>
class zero
{
};
void dis(int num) throw()
{
if(num>0)
cout<<"+ve number";
else
if(num<0)
cout<<"-ve number";
else
throw zero();
//unspecified exception
}
//this is automatically called whenever
an unexpected exception occurs
void MyUnexpected()
{
cout<<”My unexpected handler is
invoked”;
exit(34);
}
void main()
{
int num;
cout<<"Enter any number";
cin>>num;
set_unexpected(MyUnexpected);
//user defined handler
22. 09/04/1322 VIT - SCSE
try
{
dis(num);
}
catch(...)
{
cout<<"catch all exceptions";
}
cout<<endl<<"end of main";
getch();
}
Output 1:
Enter any number 10
+ve number
end of main
Output 2:
Enter any number -5
-ve number
end of main
Output 1:
Enter any number 0
My unexpected handler is invoked