SlideShare a Scribd company logo
C++ ala
Exceptions are run time anomalies or unusual conditions that a
program may encounter during execution.
Conditions such as
● Division by zero
● Access to an array outside of its bounds
● Running out of memory
● Running out of disk space
It was not a part of original C++.
It is a new feature added to ANSI C++.
C++ ala
Exception handling mechanism
● Find the problem (Hit the exception)
● Inform that an error has occurred (Throw the exception)
● Receive the error information (Catch the exception)
● Take corrective action (handle the exception)
It is basically build
upon three keywords
● Try
● Throw
● Catch
try block
Detects and
throws an
exception
catch block
Catch and handle
the exception
Exception object
The keyword try is used to preface a block of
statements which may generate exceptions.
When an exception is detected, it is thrown using a
throw statement in the try block.
A catch block defined by the keyword ‘catch’ catches the
exception and handles it appropriately.
The catch block that catches an exception must
immediately follow the try block that throws the
exception.
try
{
…
throw exception;
…
}
catch(type arg)
{
…
…
…
}
/* Block of statements
which detect and
throws an exception */
// catch exception
/* Block of statement
that handles the
exception */
Exceptions are objects used to transmit information about a
problem.
If the type of the object thrown matches the arg type in the
catch statement, the catch block is executed.
If they do not match, the program is aborted using the abort()
function (default).
 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.
Invoke function
try block
Invokes a
function that
contains an
exception
catch block
Catch and handle
the exception
throw point
Function that
causes an
exception
Throw exception
C++ ala
The throw statement can have one of the following 3 forms
● throw(exception)
● throw exception
● throw //used to re-throw a exception
The operand object exception can be of any type, including
constant.
It is also possible to throw an object not intended for error
handling.
Throw point can be in a deeply nested scope within a try block or
in a deeply nested function call.
In any case, control is transferred to the catch statement.
C++ ala
The type indicates the
type of exception the
catch block handles.
the parameter arg is an
optional parameter
name.
The catch statement
catches an exception
whose type matches
with the type of the
catch argument.
catch(type arg)
{
…
…
…
}
If the parameter in the catch statement is named, then the
parameter can be used in the exception handling code.
If a catch statement does not match the exception it is
skipped.
More than one catch statement can be associated with a
try block.
try
{
throw exception;
}
catch(type1 arg)
{
// catch block 1
}
catch(type2 arg)
{
// catch block 2
}
…
…
catch(typeN arg)
{
// catch block N
}
When an exception is thrown, the exception handlers
are searched in order for a match.
The first handler that yields a match is executed.
If several catch statement matches the type of an exception
the first handler that matches the exception type is executed.
Catch all exception
catch (…)
{
// statement for processing
// all exception
}
C++ ala
A handler may decide to rethrow the exception caught
without processing it.
In such a case we have to invoke throw without any
arguments as shown below
● throw;
This causes the current exception to be thrown to the next
enclosing try/catch sequence and is caught by a catch statement
listed after the enclosing try block.
 It is possible to restrict a function to throw certain specific
exceptions by adding a throw list clause to the function
definition.
It is possible to restrict a function to throw certain specific
exceptions by adding a throw list clause to the function
definition.
type function(arg-list) throw(type-list)
{
… … …
… … …
… … …
}
The type-list specifies the type of exception that may be
thrown.
Throwing any other kind of exception will cause abnormal
program termination.
If you want to prevent a function from throwing any exception,
you may do so by making the type-list empty.
C++ ala

More Related Content

What's hot (20)

PDF
14 exception handling
jigeno
 
PPTX
Exception handling chapter15
Kumar
 
PPT
Exception handling and templates
farhan amjad
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
Exception handling
Abhishek Pachisia
 
PDF
Exception Handling in the C++ Constructor
Somenath Mukhopadhyay
 
PPTX
exception handling in cpp
gourav kottawar
 
PPTX
Exception handling in java
yugandhar vadlamudi
 
PDF
Exception handling
Ravi Sharda
 
PPT
Exception Handling
backdoor
 
PPT
Exception handler
dishni
 
PPT
Exception handling in java
Pratik Soares
 
PPT
Week7 exception handling
Alpesh Oza
 
PPT
Understanding Exception Handling in .Net
Mindfire Solutions
 
PPS
Exception handling in c programming
Raza Najam
 
PPTX
Exception handling in c++
imran khan
 
PPTX
Presentation1
Anul Chaudhary
 
PPTX
130410107010 exception handling
Hemant Chetwani
 
PPT
Exception handling
Iblesoft
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
14 exception handling
jigeno
 
Exception handling chapter15
Kumar
 
Exception handling and templates
farhan amjad
 
Exception Handling
Reddhi Basu
 
Exception handling
Abhishek Pachisia
 
Exception Handling in the C++ Constructor
Somenath Mukhopadhyay
 
exception handling in cpp
gourav kottawar
 
Exception handling in java
yugandhar vadlamudi
 
Exception handling
Ravi Sharda
 
Exception Handling
backdoor
 
Exception handler
dishni
 
Exception handling in java
Pratik Soares
 
Week7 exception handling
Alpesh Oza
 
Understanding Exception Handling in .Net
Mindfire Solutions
 
Exception handling in c programming
Raza Najam
 
Exception handling in c++
imran khan
 
Presentation1
Anul Chaudhary
 
130410107010 exception handling
Hemant Chetwani
 
Exception handling
Iblesoft
 
Exception Handling in JAVA
SURIT DATTA
 

Similar to C++ ala (20)

PPTX
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
 
PDF
Exceptions and Exception Handling in C++
IRJET Journal
 
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
PPTX
exception handling
rajshreemuthiah
 
PPTX
Web technology
Siva Priya
 
PPTX
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
PPT
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPTX
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
PPT
Handling
Amit Vats
 
PPT
Exception handling
zindadili
 
PPT
Unit 5 Java
arnold 7490
 
PPTX
6-Exception Handling and Templates.pptx
SatyamMishra237306
 
PPTX
Exception handling
Waqas Abbasi
 
PPTX
exception%20handlingcpp.pptx
ansariparveen06
 
PPTX
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
 
PDF
EXCEPTION HANDLING in C++
Prof Ansari
 
PPT
Exceptions in java
Manav Prasad
 
PPT
Java: Exception
Tareq Hasan
 
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
 
Exceptions and Exception Handling in C++
IRJET Journal
 
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
exception handling
rajshreemuthiah
 
Web technology
Siva Priya
 
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
F6dc1 session6 c++
Mukund Trivedi
 
Exception Handling in Java
lalithambiga kamaraj
 
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
Handling
Amit Vats
 
Exception handling
zindadili
 
Unit 5 Java
arnold 7490
 
6-Exception Handling and Templates.pptx
SatyamMishra237306
 
Exception handling
Waqas Abbasi
 
exception%20handlingcpp.pptx
ansariparveen06
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
 
EXCEPTION HANDLING in C++
Prof Ansari
 
Exceptions in java
Manav Prasad
 
Java: Exception
Tareq Hasan
 
Ad

Recently uploaded (20)

PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
PPTX
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PPTX
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
PDF
IEEE EMBC 2025 「Improving electrolaryngeal speech enhancement via a represent...
NU_I_TODALAB
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
Inventory management chapter in automation and robotics.
atisht0104
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
Information Retrieval and Extraction - Module 7
premSankar19
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
IEEE EMBC 2025 「Improving electrolaryngeal speech enhancement via a represent...
NU_I_TODALAB
 
Ad

C++ ala

  • 2. Exceptions are run time anomalies or unusual conditions that a program may encounter during execution. Conditions such as ● Division by zero ● Access to an array outside of its bounds ● Running out of memory ● Running out of disk space It was not a part of original C++. It is a new feature added to ANSI C++.
  • 4. Exception handling mechanism ● Find the problem (Hit the exception) ● Inform that an error has occurred (Throw the exception) ● Receive the error information (Catch the exception) ● Take corrective action (handle the exception)
  • 5. It is basically build upon three keywords ● Try ● Throw ● Catch try block Detects and throws an exception catch block Catch and handle the exception Exception object
  • 6. The keyword try is used to preface a block of statements which may generate exceptions. When an exception is detected, it is thrown using a throw statement in the try block. A catch block defined by the keyword ‘catch’ catches the exception and handles it appropriately. The catch block that catches an exception must immediately follow the try block that throws the exception.
  • 7. try { … throw exception; … } catch(type arg) { … … … } /* Block of statements which detect and throws an exception */ // catch exception /* Block of statement that handles the exception */
  • 8. Exceptions are objects used to transmit information about a problem. If the type of the object thrown matches the arg type in the catch statement, the catch block is executed. If they do not match, the program is aborted using the abort() function (default).  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.
  • 9. Invoke function try block Invokes a function that contains an exception catch block Catch and handle the exception throw point Function that causes an exception Throw exception
  • 11. The throw statement can have one of the following 3 forms ● throw(exception) ● throw exception ● throw //used to re-throw a exception The operand object exception can be of any type, including constant. It is also possible to throw an object not intended for error handling. Throw point can be in a deeply nested scope within a try block or in a deeply nested function call. In any case, control is transferred to the catch statement.
  • 13. The type indicates the type of exception the catch block handles. the parameter arg is an optional parameter name. The catch statement catches an exception whose type matches with the type of the catch argument. catch(type arg) { … … … }
  • 14. If the parameter in the catch statement is named, then the parameter can be used in the exception handling code. If a catch statement does not match the exception it is skipped. More than one catch statement can be associated with a try block.
  • 15. try { throw exception; } catch(type1 arg) { // catch block 1 } catch(type2 arg) { // catch block 2 } … … catch(typeN arg) { // catch block N }
  • 16. When an exception is thrown, the exception handlers are searched in order for a match. The first handler that yields a match is executed. If several catch statement matches the type of an exception the first handler that matches the exception type is executed. Catch all exception catch (…) { // statement for processing // all exception }
  • 18. A handler may decide to rethrow the exception caught without processing it. In such a case we have to invoke throw without any arguments as shown below ● throw; This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after the enclosing try block.  It is possible to restrict a function to throw certain specific exceptions by adding a throw list clause to the function definition.
  • 19. It is possible to restrict a function to throw certain specific exceptions by adding a throw list clause to the function definition. type function(arg-list) throw(type-list) { … … … … … … … … … } The type-list specifies the type of exception that may be thrown. Throwing any other kind of exception will cause abnormal program termination. If you want to prevent a function from throwing any exception, you may do so by making the type-list empty.