SlideShare a Scribd company logo
EXCEPTION HANDLING
Pranali P Chaudhari
Contents
 Errors and Exception
 Exception Handling Mechanism
 Try, Throw and Catch
 Re-throwing an Exception
 Specifying Exceptions
Quiz 1
 What is an error?
 An error is a term used to describe any issue that arises
unexpectedly and results in incorrect output.
 What are the different types of errors?
 Logical error:
 Occur due to poor understanding of problem or solution
procedure.
 Syntactic error:
 Arise due to poor understanding of the language itself.
 What is an exception?
 Exceptions are run time anomalies or unusual conditions
that a program may encounter while executing.
Exception Handling
 Exceptions are of two types:
 Synchronous exceptions
 The exceptions which occur during the program
execution due to some fault in the input data are known
as synchronous exceptions.
 For example: errors such as out of range, overflow,
underflow.
 Asynchronous exceptions.
 The exceptions caused by events or faults unrelated
(external) to the program and beyond the control of the
program are called asynchronous exceptions.
 For example: errors such as keyboard interrupts,
hardware malfunctions, disk failure.
Exception Handling Mechanism
 Exception handling mechanism provides a
means to detect and report an exception
circumstances.
 Find the problem (Hit the exception)
 Inform that an error has occurred (Throw the
exception)
 Receive the error information (Catch the
exception)
 Take corrective actions (Handle the
exception)
 The error handling consists of two segments
Exception Handling Mechanism
 The exception handling mechanism is built
upon three keywords:
 Try
 Is used to preface a block of statements which may
generate exceptions.
 Throw
 When an exception is detected, it is thrown using a
throw statement in the try block.
 Catch
 A catch block defined by the keyword catch catches
the exception thrown by the throw statement in the try
block and handles it appropriately.
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.
 If the type of object thrown
matches the arg type in the
catch statment the catch
block is executed.
 Otherwise the program is
terminated with the help of
abort( ) function.
Try block throwing an exception
int main()
{
int a,b;
cout<<“enter the values of a
and b :”;
cin>>a;
cin>>b;
int x = a-b;
try
{
if(x != 0)
{
cout<<“Result (a/x) =“
<< a/x;
}
else
{
throw(x);
}
}
catch(int i)
{
cout<<“Exception Caught
: x = “ << x << “n”;
}
return 0;
}
Exceptions thrown by functions
 Mostly
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.
Exceptions thrown by functions
void divide(int x, int y, int z)
{
if((x-y) != 0)
{
int R = z/(x-y);
cout << “Result = “ << R << “n”;
}
else
{
throw (x-y);
}
}
Exceptions thrown by functions
int main()
{
try
{
divide(10,20,30);
divide(10,10,20);
}
catch(int i)
{
cout << “n Exception caught” ;
}
return 0;
}
Throwing Mechanism
 When an exception is desired to be handled is
detected, it is thrown using the throw statement.
 Throw statement has one of the following forms:
 throw(exception);
 throw exception;
 throw;
 The operand object exception may be of any type,
including constants.
Catching Mechanism
 A catch block looks like a function definition:
catch(type arg)
{
// statements for managing exceptions.
}
 The type indicates the type of exception that catch block
handles.
 The catch statement catches an exception whose type
matches with the type of catch argument.
Multiple Catch Statements
 Multiple catch statements can be associated with a
try block.
 When an exception is thrown, the exception
handlers are searched for an appropriate match.
 The first handler that yields the match is executed.
 After executing the handler, the controls goes to the
first statement after the last catch block for that try.
Multiple Catch Statements
void test(int x)
{
try
{
if (x==1) throw x;
else
if(x==0) throw ‘x’;
else
if(x== -1) throw 1.0;
cout<<“nEnd of try-block”;
}
}
catch(char c) // catch 1
{
cout<<“nCaught a character”;
}
catch(int m) // catch 2
{
cout<<“nCaught an integer”;
}
catch(double d) // catch 3
{
cout<<“nCaught a double”;
}
cout<<“n End of try-catch block”;
Multiple Catch Statements
int main( )
{
cout<<“n x = =1”;
test(1);
cout<<“n x = = 0”;
test(0);
cout<<“n x = = -1”;
test(-1);
cout<<“n x = =2”;
test(2);
return 0;
}
x == 1
Caught an integer
End of try-catch system
x == 0
Caught a character
End of try-catch system
x == -1
Caught a double
End of try-catch system
x == 2
End of try-block
End of try-catch system
Catch all Exceptions
 Sometimes it is not possible to anticipate all
possible types of exceptions and therefore not able
to design independent catch handlers to catch
them.
 A catch statement can also force to catch all
exceptions instead of a certain type alone.
 Syntax:
catch (…)
{
// statements for processing all exceptions.
}
Catch all Exceptions
void test(int x)
{
try
{
if (x==1) throw x;
else
if(x==0) throw ‘x’;
else
if(x== -1) throw 1.0;
cout<<“nEnd of try-
block”;
}
}
catch(…)
{
cout<<“n Caught an
exception”;
int main( )
{
cout<<“nTesting generic
catch”;
test(1);
test(0);
test(-1);
test(2);
return 0;
}
Re-throwing an Exception
 A handler can re-throw the exception caught without
processing it.
 This can be done using throw without any arguments.
 Here the current exception is thrown to the next enclosing
try/catch block.
 Every time when an exception is re-thrown it will not be
caught by the same catch statements rather it will be
caught by the catch statements outside the try catch block.
Re-throwing an Exception
void divide(double x, double y)
{
cout<<“Inside Function”;
try
{
if(y = =0.0)
throw y;
else
cout<<“Division = “ <<x/y<<“n”;
}
catch(double)
{
cout<<“nCaught double inside function”;
throw;
}
cout<<“n End of function”;
}
int main()
{
cout<<“n Inside main”;
try
{
divide(10.5, 2.0);
divide(20.0, 0.0);
}
catch(double)
{
cout<<“n Caught double inside
main”;
}
cout<<“n End of main”;
return 0;
}
Specifying Exceptions
 It is possible to restrict a function to throw only certain
specified exceptions.
 This is done by adding a throw list clause to the function
definition.
type function(arg-list) throw (type-list)
{
.......
.......
}
 The type-list specifies the type of exceptions that may be
thrown.
 Throwing other type of exceptions cause abnormal termination
of program.
Specifying Exceptions
void test(int x) throw (int, double)
{
if (x==0) throw ‘x’;
else
if(x==1) throw x;
else
if(x== -1) throw 1.0;
cout<<“n End of function block”;
}
int main( )
{
try
{
cout<<“nTesting throw restrictions”;
cout<<“n x==0”;
test(0);
cout<<“n x==1”;
test(1);
cout<<“n x== -1”;
test(-1);
cout<<“n x== 2”;
test(2);
}
Catch(char c)
{
cout<<“n Caught a character”;
}
Catch(int m)
{
cout<<“n Caught a integer”;
}
Catch(double d)
{
cout<<“n Caught a double”;
}
Cout<<“n End of try catch block”;
return 0;
Summary
 ______ are peculiar problems that a program may encounter at run
time.
 Exceptions are of two types _____ and ______.
 An exception is caused by a faulty statement in ____ block, which is
caught by _____ block.
 We can place two or more catch blocks to catch and handle multiple
types of exceptions. (True/ False).
 It is also possible to make a catch statement to catch all types of
exception. (True/ False)
 We cannot restrict a function to throw a specified exceptions. (True /
Short Answer Questions
 What is an exception?
 Exceptions are run time anomalies or unusual conditions
that a program may encounter while executing.
 How is exception handled in C++?
 In C++ the exception is handled using the three
keywords try, throw and catch. Or try-catch mechanism.
 What are the advantages of using exception
handling mechanism in a program?
 The purpose of exception handling mechanism is to
provide a means to detect and report an exceptional
circumstances so that appropriate action can be taken
and prevent abnormal termination of program.
Short Answer Questions
 When should a program throw an exception?
 There are some situation when a program come across
unexpected errors and cause abnormal termination of
program. To handle such errors and prevent program
from termination exceptions are thrown and handled.
 What should be placed inside the try block?
 The statement that may generate an exception are
placed in the try block.
 When do we use multiple catch handlers?
 Multiple catch handlers are used in a situation where a
program has more than one condition to throw and
exception.
Short Answer Questions
 Explain under what circumstances the
following statements would be used:
 throw;
 Re-throwing an exception.
 void fun1(float x) throw()
 Prevent a function from throwing any exception.
 catch( ... )
 Used to catch all types of exceptions.
References
 Object Oriented Programming with C++ by E.
Balagurusamy.
END OF UNIT ....
Ad

More Related Content

What's hot (20)

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Exception handling
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 

Similar to Exception handling (20)

22 scheme OOPs with C++ BCS306B_module5.pdf
22 scheme  OOPs with C++ BCS306B_module5.pdf22 scheme  OOPs with C++ BCS306B_module5.pdf
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Exception handling
Exception handlingException handling
Exception handling
Waqas Abbasi
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
. .
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
Rajan Shah
 
Exceptions in C++exception handling in C++, computer programming.ppt
Exceptions in C++exception handling in C++, computer programming.pptExceptions in C++exception handling in C++, computer programming.ppt
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
Janki Shah
 
Java unit3
Java unit3Java unit3
Java unit3
Abhishek Khune
 
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
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PACKAGES, INTERFACES AND EXCEPTION HANDLINGPACKAGES, INTERFACES AND EXCEPTION HANDLING
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
Syed Bahadur Shah
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception Handling
Exception HandlingException Handling
Exception Handling
PRN USM
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
Ashwin Shiv
 
Exception Handling in java masters of computer application
Exception Handling in java masters of computer applicationException Handling in java masters of computer application
Exception Handling in java masters of computer application
xidileh999
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
22 scheme OOPs with C++ BCS306B_module5.pdf
22 scheme  OOPs with C++ BCS306B_module5.pdf22 scheme  OOPs with C++ BCS306B_module5.pdf
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Exception handling
Exception handlingException handling
Exception handling
Waqas Abbasi
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
. .
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
Rajan Shah
 
Exceptions in C++exception handling in C++, computer programming.ppt
Exceptions in C++exception handling in C++, computer programming.pptExceptions in C++exception handling in C++, computer programming.ppt
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
Janki Shah
 
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
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PACKAGES, INTERFACES AND EXCEPTION HANDLINGPACKAGES, INTERFACES AND EXCEPTION HANDLING
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception Handling
Exception HandlingException Handling
Exception Handling
PRN USM
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
Ashwin Shiv
 
Exception Handling in java masters of computer application
Exception Handling in java masters of computer applicationException Handling in java masters of computer application
Exception Handling in java masters of computer application
xidileh999
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
Ad

More from Pranali Chaudhari (9)

Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
 
Templates
TemplatesTemplates
Templates
Pranali Chaudhari
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Pranali Chaudhari
 
Ad

Recently uploaded (20)

new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 

Exception handling

  • 2. Contents  Errors and Exception  Exception Handling Mechanism  Try, Throw and Catch  Re-throwing an Exception  Specifying Exceptions
  • 3. Quiz 1  What is an error?  An error is a term used to describe any issue that arises unexpectedly and results in incorrect output.  What are the different types of errors?  Logical error:  Occur due to poor understanding of problem or solution procedure.  Syntactic error:  Arise due to poor understanding of the language itself.  What is an exception?  Exceptions are run time anomalies or unusual conditions that a program may encounter while executing.
  • 4. Exception Handling  Exceptions are of two types:  Synchronous exceptions  The exceptions which occur during the program execution due to some fault in the input data are known as synchronous exceptions.  For example: errors such as out of range, overflow, underflow.  Asynchronous exceptions.  The exceptions caused by events or faults unrelated (external) to the program and beyond the control of the program are called asynchronous exceptions.  For example: errors such as keyboard interrupts, hardware malfunctions, disk failure.
  • 5. Exception Handling Mechanism  Exception handling mechanism provides a means to detect and report an exception circumstances.  Find the problem (Hit the exception)  Inform that an error has occurred (Throw the exception)  Receive the error information (Catch the exception)  Take corrective actions (Handle the exception)  The error handling consists of two segments
  • 6. Exception Handling Mechanism  The exception handling mechanism is built upon three keywords:  Try  Is used to preface a block of statements which may generate exceptions.  Throw  When an exception is detected, it is thrown using a throw statement in the try block.  Catch  A catch block defined by the keyword catch catches the exception thrown by the throw statement in the try block and handles it appropriately.
  • 7. 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.  If the type of object thrown matches the arg type in the catch statment the catch block is executed.  Otherwise the program is terminated with the help of abort( ) function.
  • 8. Try block throwing an exception int main() { int a,b; cout<<“enter the values of a and b :”; cin>>a; cin>>b; int x = a-b; try { if(x != 0) { cout<<“Result (a/x) =“ << a/x; } else { throw(x); } } catch(int i) { cout<<“Exception Caught : x = “ << x << “n”; } return 0; }
  • 9. Exceptions thrown by functions  Mostly 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.
  • 10. Exceptions thrown by functions void divide(int x, int y, int z) { if((x-y) != 0) { int R = z/(x-y); cout << “Result = “ << R << “n”; } else { throw (x-y); } }
  • 11. Exceptions thrown by functions int main() { try { divide(10,20,30); divide(10,10,20); } catch(int i) { cout << “n Exception caught” ; } return 0; }
  • 12. Throwing Mechanism  When an exception is desired to be handled is detected, it is thrown using the throw statement.  Throw statement has one of the following forms:  throw(exception);  throw exception;  throw;  The operand object exception may be of any type, including constants.
  • 13. Catching Mechanism  A catch block looks like a function definition: catch(type arg) { // statements for managing exceptions. }  The type indicates the type of exception that catch block handles.  The catch statement catches an exception whose type matches with the type of catch argument.
  • 14. Multiple Catch Statements  Multiple catch statements can be associated with a try block.  When an exception is thrown, the exception handlers are searched for an appropriate match.  The first handler that yields the match is executed.  After executing the handler, the controls goes to the first statement after the last catch block for that try.
  • 15. Multiple Catch Statements void test(int x) { try { if (x==1) throw x; else if(x==0) throw ‘x’; else if(x== -1) throw 1.0; cout<<“nEnd of try-block”; } } catch(char c) // catch 1 { cout<<“nCaught a character”; } catch(int m) // catch 2 { cout<<“nCaught an integer”; } catch(double d) // catch 3 { cout<<“nCaught a double”; } cout<<“n End of try-catch block”;
  • 16. Multiple Catch Statements int main( ) { cout<<“n x = =1”; test(1); cout<<“n x = = 0”; test(0); cout<<“n x = = -1”; test(-1); cout<<“n x = =2”; test(2); return 0; } x == 1 Caught an integer End of try-catch system x == 0 Caught a character End of try-catch system x == -1 Caught a double End of try-catch system x == 2 End of try-block End of try-catch system
  • 17. Catch all Exceptions  Sometimes it is not possible to anticipate all possible types of exceptions and therefore not able to design independent catch handlers to catch them.  A catch statement can also force to catch all exceptions instead of a certain type alone.  Syntax: catch (…) { // statements for processing all exceptions. }
  • 18. Catch all Exceptions void test(int x) { try { if (x==1) throw x; else if(x==0) throw ‘x’; else if(x== -1) throw 1.0; cout<<“nEnd of try- block”; } } catch(…) { cout<<“n Caught an exception”; int main( ) { cout<<“nTesting generic catch”; test(1); test(0); test(-1); test(2); return 0; }
  • 19. Re-throwing an Exception  A handler can re-throw the exception caught without processing it.  This can be done using throw without any arguments.  Here the current exception is thrown to the next enclosing try/catch block.  Every time when an exception is re-thrown it will not be caught by the same catch statements rather it will be caught by the catch statements outside the try catch block.
  • 20. Re-throwing an Exception void divide(double x, double y) { cout<<“Inside Function”; try { if(y = =0.0) throw y; else cout<<“Division = “ <<x/y<<“n”; } catch(double) { cout<<“nCaught double inside function”; throw; } cout<<“n End of function”; } int main() { cout<<“n Inside main”; try { divide(10.5, 2.0); divide(20.0, 0.0); } catch(double) { cout<<“n Caught double inside main”; } cout<<“n End of main”; return 0; }
  • 21. Specifying Exceptions  It is possible to restrict a function to throw only certain specified exceptions.  This is done by adding a throw list clause to the function definition. type function(arg-list) throw (type-list) { ....... ....... }  The type-list specifies the type of exceptions that may be thrown.  Throwing other type of exceptions cause abnormal termination of program.
  • 22. Specifying Exceptions void test(int x) throw (int, double) { if (x==0) throw ‘x’; else if(x==1) throw x; else if(x== -1) throw 1.0; cout<<“n End of function block”; } int main( ) { try { cout<<“nTesting throw restrictions”; cout<<“n x==0”; test(0); cout<<“n x==1”; test(1); cout<<“n x== -1”; test(-1); cout<<“n x== 2”; test(2); } Catch(char c) { cout<<“n Caught a character”; } Catch(int m) { cout<<“n Caught a integer”; } Catch(double d) { cout<<“n Caught a double”; } Cout<<“n End of try catch block”; return 0;
  • 23. Summary  ______ are peculiar problems that a program may encounter at run time.  Exceptions are of two types _____ and ______.  An exception is caused by a faulty statement in ____ block, which is caught by _____ block.  We can place two or more catch blocks to catch and handle multiple types of exceptions. (True/ False).  It is also possible to make a catch statement to catch all types of exception. (True/ False)  We cannot restrict a function to throw a specified exceptions. (True /
  • 24. Short Answer Questions  What is an exception?  Exceptions are run time anomalies or unusual conditions that a program may encounter while executing.  How is exception handled in C++?  In C++ the exception is handled using the three keywords try, throw and catch. Or try-catch mechanism.  What are the advantages of using exception handling mechanism in a program?  The purpose of exception handling mechanism is to provide a means to detect and report an exceptional circumstances so that appropriate action can be taken and prevent abnormal termination of program.
  • 25. Short Answer Questions  When should a program throw an exception?  There are some situation when a program come across unexpected errors and cause abnormal termination of program. To handle such errors and prevent program from termination exceptions are thrown and handled.  What should be placed inside the try block?  The statement that may generate an exception are placed in the try block.  When do we use multiple catch handlers?  Multiple catch handlers are used in a situation where a program has more than one condition to throw and exception.
  • 26. Short Answer Questions  Explain under what circumstances the following statements would be used:  throw;  Re-throwing an exception.  void fun1(float x) throw()  Prevent a function from throwing any exception.  catch( ... )  Used to catch all types of exceptions.
  • 27. References  Object Oriented Programming with C++ by E. Balagurusamy.
  • 28. END OF UNIT ....