0% found this document useful (0 votes)
81 views34 pages

Ch-6 Exception Handling

The document discusses object-oriented programming using C++ and exception handling. It provides 3 key points: 1. Exceptions are abnormal conditions that arise during program execution. Exception handling allows programs to gracefully handle errors. 2. Exception handling in C++ uses try, catch, and throw keywords. Code that may throw exceptions is placed in a try block. Exceptions are passed to catch blocks to be processed. 3. Examples demonstrate throwing different types of exceptions, catching multiple exceptions, and rethrowing exceptions. Exception objects are destroyed before control passes to catch blocks.

Uploaded by

Prince Kumar
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)
81 views34 pages

Ch-6 Exception Handling

The document discusses object-oriented programming using C++ and exception handling. It provides 3 key points: 1. Exceptions are abnormal conditions that arise during program execution. Exception handling allows programs to gracefully handle errors. 2. Exception handling in C++ uses try, catch, and throw keywords. Code that may throw exceptions is placed in a try block. Exceptions are passed to catch blocks to be processed. 3. Examples demonstrate throwing different types of exceptions, catching multiple exceptions, and rethrowing exceptions. Exception objects are destroyed before control passes to catch blocks.

Uploaded by

Prince Kumar
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/ 34

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-1


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Object-oriented Programming using
C++
CSP-157
Prepared by Ms. Jasleen Kaur
Exception Handling DISCOVER . LEARN . EMPOWER
CHAPTER -6
Course Outcome
CO Title Level
Number

CO1 provide the environment that allows students to Remember


understand object-oriented programming Concepts.   Will be covered in this
CO2 demonstrate basic experimental skills for Understand lecture
differentiating between object-oriented and  
procedural programming paradigms and the
advantages of object-oriented programs.

CO3 Ability to demonstrate their coding skill on complex Understand


programming concepts and use it for generating
solutions for engineering and mathematical problems.
CO4 Students will develop skills to design the application of Understand
classes, objects, constructors, destructors, inheritance,  
operator overloading and polymorphism, pointers,
virtual functions, templates, exception handling, file
operations and handling
2
• Exceptions
• Handling exceptions
• Re-throw

CONTENTS

Exceptions are abnormal conditions during run


time or compile time. Exception Handling is a very
essential concept in c++.

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”.

Figure 6.1 SYNTAX[1]

7
EXCEPTION HANDLING

Figure 6.2 Program made on Dev-C++ 8


TRY, THROW, CATCH
The code that is likely to throw exceptions is enclosed in the “try” block. The try block can also contain a “throw” statement
that is used to explicitly throw exceptions. The “throw” statement consists of a keyword “throw” followed by a parameter
which is the exception name. This parameter is then passed to the catch block.
The exception thrown in the “try” block is passed on to the “catch” block. The catch block contains the code to process the
thrown exception. It can contain merely a message or an entire code block to process the exception such that the normal flow
of the program is not hindered.
It is not necessary that each try block should be followed by only one catch block. If our try block code contains the statements
that can throw more than one exceptions, then we can have multiple catch blocks wherein each catch block provides
processing for each of the exceptions.

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++

Figure 6.3 Std exceptions C++[1] 11


STD EXCEPTIONS C++

Figure 6.4 Std exceptions C++[1] 12


STD EXCEPTIONS C++

Figure 6.5 Std exceptions C++[1] 13


EXAMPLE 2

Figure 6.6 Program made on Dev-C++ 14


EXAMPLE 2

Figure 6.7 Program made on Dev-C++ 15


EXAMPLE 2

Figure 6.8 Program made on Dev-C++ 16


EXCEPTION IN FUNCTION

Figure 6.9 Program made on Dev-C++ 17


EXCEPTION IN FUNCTION

Figure 6.10 Program made on Dev-C++ 18


bad_alloc EXCEPTION

Figure 6.11 Program made on Dev-C++ 19


bad_alloc EXCEPTION

Figure 6.12 Program made on Dev-C++ 20


EXCEPTION IN A CLASS

Figure 6.13 Program made on Dev-C++ 21


EXCEPTION IN A CLASS

Figure 6.14 Program made on Dev-C++ 22


EXCEPTION IN A CLASS

Figure 6.15 Program made on Dev-C++ 23


MULTIPLE CATCH

Figure 6.16 Program made on Dev-C++ 24


MULTIPLE CATCH

Figure 6.17 PROGRAM MADE ON Dev-C++] 25


MULTIPLE CATCH

Figure 6.18 PROGRAM MADE ON Dev-C++ 26


GENERALIZED CATCH (…)
If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the
exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers:
try {
// code here
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }

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 (…)

Figure 6.19 PROGRAM MADE ON Dev-C++] 28


GENERALISED CATCH (…)

Figure 6.20 PROGRAM MADE ON Dev-C++] 29


RETHROW EXCEPTION

A function can also re-throw a function using


same “throw; “. A function can handle a part
and can ask the caller to handle remaining.

Figure 6.21 Rethrow exception [2] 30


Assessment Pattern
Section-A
1. (a) Define synchronous exceptions
(b) What is the difference between exceptions and normal errors?
(c) Differentiate between try, catch and throw
Section-B
2. Write a C++ program to demonstrate the concept of throwing an integer exception.
3. WAP in C++ to demonstrate multiple catch
4. Write a C++ program to use generalised exception.

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]

You might also like