Introduction to C++ Programming(BPLCK205D)_Class PPT_Module-5
Introduction to C++ Programming(BPLCK205D)_Class PPT_Module-5
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
Thank you