SlideShare a Scribd company logo
1 of 20
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++.
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
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.
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
}
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

14 exception handling
14 exception handling14 exception handling
14 exception handlingjigeno
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cppgourav kottawar
 
Exception handling
Exception handlingException handling
Exception handlingRavi Sharda
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .NetMindfire Solutions
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programmingRaza Najam
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handlingHemant Chetwani
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 

What's hot (20)

14 exception handling
14 exception handling14 exception handling
14 exception handling
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handler
Exception handler Exception handler
Exception handler
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programming
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Presentation1
Presentation1Presentation1
Presentation1
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Similar to C++ ala

Exceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptxExceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptxestorebackupr
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref. .
 
Exception Handling
Exception HandlingException Handling
Exception HandlingPRN USM
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11Terry Yoast
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdfMaddalaSeshu
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRKiran Munir
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapriyankazope
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++IRJET Journal
 

Similar to C++ ala (20)

Exceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptxExceptions in C++ Object Oriented Programming.pptx
Exceptions in C++ Object Oriented Programming.pptx
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Chap12
Chap12Chap12
Chap12
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java unit3
Java unit3Java unit3
Java unit3
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
7_exception.pdf
7_exception.pdf7_exception.pdf
7_exception.pdf
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++
 

Recently uploaded

Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

C++ ala

  • 1.
  • 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++.
  • 3.
  • 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
  • 10.
  • 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.
  • 12.
  • 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 }
  • 17.
  • 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.