0% found this document useful (0 votes)
7 views

Introduction to C++ Programming(BPLCK205D)_Class PPT_Module-5

The document provides an introduction to exception handling in C++, detailing its importance and mechanisms using keywords such as try, catch, and throw. It explains the types of exceptions, their detection, and the separation of error handling code from normal execution paths. Additionally, it highlights the benefits of exception handling, including improved program stability and clearer error management.

Uploaded by

Antheesh R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction to C++ Programming(BPLCK205D)_Class PPT_Module-5

The document provides an introduction to exception handling in C++, detailing its importance and mechanisms using keywords such as try, catch, and throw. It explains the types of exceptions, their detection, and the separation of error handling code from normal execution paths. Additionally, it highlights the benefits of exception handling, including improved program stability and clearer error management.

Uploaded by

Antheesh R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

INTRODUCTION TO C++

PROGRAMMING
Subject Code: BPLCK205D
Module -5
Exception Handling
Introduction to Exception - Benefits of
Exception handling- Try and catch block
Throw statement- Pre-defined exceptions in
C++. Textbook 2: Chapter 13(13.2 to 13.6)
Exception Handling
 Introduction to Exception:
• The errors occurred in program may be logical errors or
syntactic errors.
• The Logical errors occurs due to poor understanding of the
problem and solution procedure.
• The Syntactic errors arise due to poor understanding of the
Language itself.
• Detection of these errors are done by using exhaustive
debugging and testing procedure.
• “Exceptions are runtime anomalies or unusual conditions
of the program which may encounter while executing them”.
Exception Handling
 Introduction to Exception:
• Exceptions are errors that occur at run time.
• They are caused by a wide variety of exceptional circumstance,
such as running out of memory, not being able to open a file, trying
to initialize an object to an impossible value, or using an out - of -
bounds index to a vector.
• An exception is an error or an expected event.
• The exception handler is a set of codes that executes when an
exception occurs.
• Exception handling is one of the most recently added features in
C++.
• Exception handling in C++ provides a better method by which the
caller of a function can be informed that some error condition has
occurred.
Exception Handling
 Introduction to Exception:
• Types of exceptions:
• There are two kinds of exceptions
1.Synchronous exceptions
2.Asynchronous exceptions
• Synchronous exceptions: Errors such as “Out-of-range
index” and “over flow” are synchronous exceptions
• Asynchronous exceptions: The errors that are generated by
any event beyond the control of the program are called
asynchronous exceptions
• The purpose of exception handling is to provide a means to
detect and report an exceptional circumstance
Exception Handling
 Introduction to Exception:
• The following keywords are used for error handling in C++.
1. Try
2. Catch
3. Throw
• An exception also known as run time errors because these
are occurs at runtime.
• The purpose of the exception handling mechanism is to
provide means to detect and report an "exceptional
circumstance" so that appropriate action can be taken.
Exception Handling
 Introduction to Exception:
• The mechanism suggests a separate error handling code that
performs the following tasks:
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).
• The error handling code basically consists of two segments,
one to detect errors and to throw exceptions, and the
other to catch the exceptions and to take appropriate
actions.
Exception Handling
 Exception Handling:
• “Exception handling is the process of responding to the
occurrence, during computation, of exceptions or
exceptional conditions requiring special processing”.
• An exception is a problem that arises during the execution of
the program.
• A C++ exceptions a type of response for an exceptional
situation that arises while a program is running, in such
types of situation as an attempt to divide by zero.
• The goal of exception handling is to create a routine that
detect and sends an exceptional condition in order to execute
suitable actions.
Exception Handling
 Exception Handling Mechanism:
• C++ exception handling mechanism is basically built upon
three keywords, namely, try, throw, and catch.
• The keyword try is used to preface a block of
statements(surrounded by braces) which may generate
exceptions.
• This block of statements is known as try block.
• When an exception is detected, it is thrown using a throw
statement in the try block.
Exception Handling
 Exception Handling Mechanism:
• A catch block defined by the
keyword catch 'catches' the
exception 'thrown' by the throw
statement in the try block, and
handles it appropriately.
• The relationship is shown in
Figure.
• The catch block that catches an
exception must immediately
follow the try block that throws
the exception. The general form
of these two blocks are as
follows:
Exception Handling
 Exception Handling Mechanism:
• Syntax:
Exception Handling
 Exception Handling Mechanism:
• When the try block throws an exception, the program control
leaves the try block and enters the catch statement of the catch
block.
• Note that exceptions are objects used to transmit information about
a problem.
• If the type of object thrown matches the arg type in the catch
statement, then catch block is executed for handling the exception.
• If they do not match, the program is aborted with the help of the
abort( ) function which is invoked by default.
• When no exception is detected and thrown, the control goes to the
statement immediately after the catch block.
• That is, the catch block is skipped. This simple try-catch
mechanism is illustrated in Program
Exception Handling
 Exception Handling Mechanism:
• A simple try-catch mechanism is illustrated in Program

The output of Program:


First Run:
Enter Values of a and b
20 165
Result(a/x) = 4
END
Second Run:
Enter Values of a and b
10 0
Exception caught: x = 0
END
Exception Handling
 Exception Handling Mechanism:
• Program detects and catches a division-by-zero problem.
• The output of first run shows a successful execution. When no exception
is thrown, the catch block is skipped and execution resumes with the first
line after the catch.
• In the second run, the denominator x becomes zero and therefore a
division-by-zero situation occurs.
• This exception is thrown using the object x.
• Since the exception object is an int type, the catch statement containing
int type argument catches the exception and displays necessary message.
• Most often, exceptions are thrown by functions that are invoked from
within the try blocks.
• The point at which the throw is executed is called the throw point.
• Once an exception is thrown to the catch block, control cannot return to
the throw point.
• This kind of relationship is shown in Figure
Exception Handling
 Exception Handling Mechanism:
Exception Handling
 Exception Handling Mechanism:
Exception Handling
 Benefits of Exception Handling Mechanism:
1. Exception handling control run time errors and avoid
abnormal termination of program execution.
2. Exception handling mechanism provide a facility to handle
exceptions i.e., by using try and catch blocks, detect the
error, throws a proper error message and complete the
execution by catching the exception.
3. Exception handling mechanism separate the error
handling code and normal code by using try-catch block.
So eliminate the need of checking the error in normal
execution path.
Exception Handling
 Exception Handling Mechanism:
A program to throw exception when j=1 otherwise
perform the subtraction of x and y.
#include<iostream.h> try
#include<conio.h> {
int main() if (j==0)
{ { cout<<“Subtraction (x-y)=”<<x-y<<“\n”; }
int x,y; else { throw(j); }
cout<<“\n Enter values of x and y\n”; }
catch (int k)
cin>>x>>y;
{
int j; cout<<“Exception caught : j =”<<j <<“\n”;
j=x>y ? 0 :1; }
OUTPUT: return 0;
Enter values of x and y }
78 }
Exception caught : j = 1
Enter values of x and y
48
Exception caught : j = 1
Textbooks
1. Bhushan Trivedi, “Programming with ANSI C++”, Oxford Press,
Second Edition, 2012.
2. Balagurusamy E, “Object Oriented Programming with C++”, Tata
McGraw Hill Education Pvt. Ltd, Fourth Edition, 2010.
Weblinks and Video Lectures (e-Resources):
1. Basics of C++ :
https://www.youtube.com/watch?v=BClS40yzssA
2. Functions of C++ : https://www.youtube.com/watch?v=p8ehAjZWjPw
Tutorial Link:
1. https://www.w3schools.com/cpp/cpp_intro.asp
2. https://www.edx.org/course/introduction-to-c-3
20

Thank you

You might also like