Ch-6 Exception Handling
Ch-6 Exception Handling
CONTENTS
3
WHAT IS EXCEPTION
• An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional
circumstance that arises while a program is running, such as an attempt to divide by zero.
• Exceptions are different, however. You can't eliminate exceptional circumstances; you can only prepare for them. Your users
will run out of memory from time to time, and the only question is what you will do. Your choices are limited to these:
Crash the program.
Inform the user and exit gracefully.
Inform the user and allow the user to try to recover and continue.
Take corrective action and continue without disturbing the user.
4
WHAT IS EXCEPTION
• Many kinds of errors can cause exceptions--problems ranging from serious hardware errors, such as a hard disk crash, to
simple programming errors, such as trying to access an out-of-bounds array element.
• When such an error occurs, the method creates an exception object and hands it off to the runtime system.
• The exception object contains information about the exception, including its type and the state of the program when the
error occurred.
• The runtime system is then responsible for finding some code to handle the error.
• In programming terminology, creating an exception object and handing it to the runtime system is called throwing an
exception.
5
EXCEPTION HANDLER
• After a method throws an exception, the runtime system leaps into action to find someone to handle the exception.
• The set of possible "someones" to handle the exception is the set of methods in the call stack of the method where the error
occurred.
• The runtime system searches backwards through the call stack, beginning with the method in which the error occurred, until
it finds a method that contains an appropriate exception handler.
• An exception handler is considered appropriate if the type of the exception thrown is the same as the type of exception
handled by the handler.
• Thus the exception bubbles up through the call stack until an appropriate handler is found and one of the calling methods
handles the exception.
• The exception handler chosen is said to catch the exception.
6
EXCEPTION HANDLING
• Errors disrupt normal execution of a program. Exception handling is very necessary, and it is the process of handling errors
or exceptions. It makes sure that the execution of the program is not affected by the exceptions and slowly handles them
without causing any issue to the program execution.
• n C++, exception handling is provided by using three constructs or keywords; namely, try, catch and throw.
• Block of code that provides a way to handle the exception is called “exception handler”.
7
EXCEPTION HANDLING
9
OBJECTS
• When an exception is thrown, all objects created inside the enclosing try block are destructed before the control is transferred to
catch block.
#include <iostream>
using namespace std;
class Test { public:
Test() { cout << "Constructor of Test " << endl; }
~Test() { cout << "Destructor of Test " << endl; } };
int main() {
try {
Test t1;
throw 10;
} catch(int i) {
cout << "Caught " << i << endl; }
}
10
STD EXCEPTIONS C++
In this case, the last handler would catch any exception thrown of a type that is neither int nor char.
After an exception has been handled by the program, execution resumes after the try-catch block, not after the throw
statement!.
27
GENERALISED CATCH (…)
31
APPLICATIONS
• First of all there are things that just can’t be done right without exceptions. Consider an error detected in a constructor; how
do you report the error? You throw an exception. That’s the basis of RAII (Resource Acquisition Is Initialization), which is
the basis of some of the most effective modern C++ design techniques: A constructor’s job is to establish the invariants for
the class (create the environment in which the member functions are to run) and that often requires the acquisition of
resources, such as memory, locks, files, sockets, etc.
• Imagine that we did not have exceptions, how would you deal with an error detected in a constructor?
32
REFERENCES
• Reference Website
[1] https://www.softwaretestinghelp.com/exception-handling-cpp/
[2] https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
[3] https://www.geeksforgeeks.org/exception-handling-c/
[4] https://www.studytonight.com/cpp/exception-handling-in-cpp.php
33
THANK YOU
For queries
Email: [email protected]