SlideShare ist ein Scribd-Unternehmen logo
1 von 22
09/04/131 VIT - SCSE
‱ Logic errors
‱ Syntactic errors
‱ Mechanisms for handling run time errors
‱ 2 types of exceptions
‱ Synchronous exception
‱ e.x: errors such as “out-of-range index” and “over-flow”
‱ Asynchronous exception
‱ e.x: such as a keyboard interrupts
Exception Handling
09/04/132 VIT - SCSE
The proposed method is only for handling synchronous
exceptions
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)
09/04/133 VIT - SCSE
try block
Detects and throw an exception
catch block
Catches and handles the exception
09/04/134 VIT - SCSE






try
{


..
throw exception;


..


..
}
catch(type arg)
{


.


.
}


.


.
09/04/135 VIT - SCSE
void main()
{
int i=0;
cin>>i;
try
{
if(i=0)
throw i;
}
catch(int i)
{
cout<<”Exception
caught: ;
}
cout<<”End”;
}
09/04/136 VIT - SCSE
Throwing mechanism
throw(exception);
throw exception;
throw;
09/04/137 VIT - SCSE
try
{
//try block
}
catch(type1 arg)
{
//catch block1
}
catch(type2 arg)
{
//catch block2
}


.


.
catch(typeN arg)
{
//catch blockN
}
Multiple catch statement
09/04/138 VIT - SCSE
void test(int x)
{
try
{
if(x= =1) throw x;
else
if(x= =0) throw ‘x’;
else
if(x= =-1 throw 1.0;
cout<<”End of try block”;
}
catch(char c)
{
cout<<”Catch a character”;
}
catch(int m)
{
cout<<”catch an
integer”;
}
catch(double d)
{
cout<<”catch a
double”;
}
cout<<”End of try –
catch system”;
}
09/04/139 VIT - SCSE
void main()
{
cout<<”Testing multiple catches”;
cout<<”x= =1 n”;
test(1);
cout<<”x= =0 n”;
test(0);
cout<<”x= =-1 n”;
test(-1);
cout<<”x= =2 n”;
test(2);
}
Catch all exceptions
catch(
.)
{
//statements
}
09/04/1310 VIT - SCSE
void test(int x)
{
try
{
if(x= =0) throw x;
if(x= =-1) throw ‘x’;
if(x= =1) throw 1.0;
}
catch(
)
{
cout<<”caught an exception”;
}
}
void main()
{
cout<<”testing generic catch”;
test(-1);
test(0);
test(1);
}
Handling Uncaught Exceptions
terminate()
set_terminate()
unexpected()
set_unexpected()
09/04/1311 VIT - SCSE
terminate()
‱the function terminate() is invoked when an exception is
raised and the handler is not found.
‱The default action for terminate is to invoke abort().
‱Such a default action causes immediate termination of the
program execution.
09/04/1312 VIT - SCSE
class excep1
{
};
class excep2
{
};
void main()
{
try
{
cout<<”Throwing uncaught exception”<<endl;
throw excep2();
}
catch(excep1) //true if throw excep1 is executed in try scope
{
//action for exception
cout<<”Exception1”;
}
//excep2 is not caught hence, program aborts here without proceeding
further
cout<<”I am not displayed”;
}
09/04/1313 VIT - SCSE
Output:
Throwing uncaught exception
Program aborted
09/04/1314 VIT - SCSE
set_terminate
user can define their own exception handler
or
user defined exception
set_terminate is defined in except.h
09/04/1315 VIT - SCSE
The program to handle uncaught exceptions with the user specified
terminate function
//all exceptions are not caught, executes MyTerminate()
#include<process.h>
#include<except.h>
class excep1
{
};
class excep2
{
};
void MyTerminate()
{
cout<<”My Terminate is invoked”;
exit(1);
}
09/04/1316 VIT - SCSE
void main()
{
set_terminate(MyTerminate); //set to our own terminate function
try
{
cout<<”Throwing uncaught exception”<<endl;
throw excep2();
}
catch(excep1) //true if throw excep1 is executed in try scope
{
//action for exception
cout<<”Caught exception1”;
}
//program abort() here, MyTransaction will be called
cout<<”I am not displayed”;
}
09/04/1317 VIT - SCSE
Output:
Throwing uncaught exception
My Terminate is invoked
09/04/1318 VIT - SCSE
unexpected()
The unexpected () function is called when a function throws an
exception not listed in its exception specification.
The program calls unexpected() which calls any user-defined
function registered by set_unexpected().
If no function is registered with set_unexpected, the
unexpected() function then invokes the terminate() function.
The prototype of the unexpected() call is
void unexpected();
the function unexpected() returns nothing (void).
But it can throw an exception through the execution of a
function registered by the set_unexpected function.
09/04/1319 VIT - SCSE
//unexpected exception
#include<except.h>
class zero
{
};
void dis(int num) throw()
{
if(num>0)
cout<<"+ve number";
else
if(num<0)
cout<<"-ve number";
else
throw zero(); //unspecified exception
}
void main()
{
int num;
cout<<"Enter any
number";
cin>>num;
try
{
dis(num);
}
catch(...)
{
cout<<"catch all
exceptions";
}
cout<<endl<<"end of
main";
getch();
}
09/04/1320 VIT - SCSE
Output 1:
Enter any number 10
+ve number
end of main
Output 2:
Enter any number -5
-ve number
end of main
Output 3:
Enter any number 0
Program aborted
09/04/1321 VIT - SCSE
set_unexcepted()
#include<process.h>
//has prototype for exit()
#include<except.h>
class zero
{
};
void dis(int num) throw()
{
if(num>0)
cout<<"+ve number";
else
if(num<0)
cout<<"-ve number";
else
throw zero();
//unspecified exception
}
//this is automatically called whenever
an unexpected exception occurs
void MyUnexpected()
{
cout<<”My unexpected handler is
invoked”;
exit(34);
}
void main()
{
int num;
cout<<"Enter any number";
cin>>num;
set_unexpected(MyUnexpected);
//user defined handler
09/04/1322 VIT - SCSE
try
{
dis(num);
}
catch(...)
{
cout<<"catch all exceptions";
}
cout<<endl<<"end of main";
getch();
}
Output 1:
Enter any number 10
+ve number
end of main
Output 2:
Enter any number -5
-ve number
end of main
Output 1:
Enter any number 0
My unexpected handler is invoked

Weitere Àhnliche Inhalte

Ähnlich wie 13 exception handling

Exceptions ref
Exceptions refExceptions ref
Exceptions ref
. .
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Handling
HandlingHandling
Handling
Amit Vats
 
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
rohitgudasi18
 

Ähnlich wie 13 exception handling (20)

Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
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
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Lecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptxLecture 09 Exception Handling(1 ) in c++.pptx
Lecture 09 Exception Handling(1 ) in c++.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Handling
HandlingHandling
Handling
 
Unit iii pds
Unit iii pdsUnit iii pds
Unit iii pds
 
java.pptx
java.pptxjava.pptx
java.pptx
 
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 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
 
SEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitationSEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitation
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 

Mehr von Docent Education (15)

17 files and streams
17 files and streams17 files and streams
17 files and streams
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
 
7 class objects
7 class objects7 class objects
7 class objects
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
 
5 array
5 array5 array
5 array
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented Programming
 
3 intro basic_elements
3 intro basic_elements3 intro basic_elements
3 intro basic_elements
 
2 Intro c++
2 Intro c++2 Intro c++
2 Intro c++
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
 

KĂŒrzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

KĂŒrzlich hochgeladen (20)

Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

13 exception handling

  • 1. 09/04/131 VIT - SCSE ‱ Logic errors ‱ Syntactic errors ‱ Mechanisms for handling run time errors ‱ 2 types of exceptions ‱ Synchronous exception ‱ e.x: errors such as “out-of-range index” and “over-flow” ‱ Asynchronous exception ‱ e.x: such as a keyboard interrupts Exception Handling
  • 2. 09/04/132 VIT - SCSE The proposed method is only for handling synchronous exceptions 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)
  • 3. 09/04/133 VIT - SCSE try block Detects and throw an exception catch block Catches and handles the exception
  • 4. 09/04/134 VIT - SCSE 

 

 try { 

.. throw exception; 

.. 

.. } catch(type arg) { 

. 

. } 

. 

.
  • 5. 09/04/135 VIT - SCSE void main() { int i=0; cin>>i; try { if(i=0) throw i; } catch(int i) { cout<<”Exception caught: ; } cout<<”End”; }
  • 6. 09/04/136 VIT - SCSE Throwing mechanism throw(exception); throw exception; throw;
  • 7. 09/04/137 VIT - SCSE try { //try block } catch(type1 arg) { //catch block1 } catch(type2 arg) { //catch block2 } 

. 

. catch(typeN arg) { //catch blockN } Multiple catch statement
  • 8. 09/04/138 VIT - SCSE void test(int x) { try { if(x= =1) throw x; else if(x= =0) throw ‘x’; else if(x= =-1 throw 1.0; cout<<”End of try block”; } catch(char c) { cout<<”Catch a character”; } catch(int m) { cout<<”catch an integer”; } catch(double d) { cout<<”catch a double”; } cout<<”End of try – catch system”; }
  • 9. 09/04/139 VIT - SCSE void main() { cout<<”Testing multiple catches”; cout<<”x= =1 n”; test(1); cout<<”x= =0 n”; test(0); cout<<”x= =-1 n”; test(-1); cout<<”x= =2 n”; test(2); } Catch all exceptions catch(
.) { //statements }
  • 10. 09/04/1310 VIT - SCSE void test(int x) { try { if(x= =0) throw x; if(x= =-1) throw ‘x’; if(x= =1) throw 1.0; } catch(
) { cout<<”caught an exception”; } } void main() { cout<<”testing generic catch”; test(-1); test(0); test(1); } Handling Uncaught Exceptions terminate() set_terminate() unexpected() set_unexpected()
  • 11. 09/04/1311 VIT - SCSE terminate() ‱the function terminate() is invoked when an exception is raised and the handler is not found. ‱The default action for terminate is to invoke abort(). ‱Such a default action causes immediate termination of the program execution.
  • 12. 09/04/1312 VIT - SCSE class excep1 { }; class excep2 { }; void main() { try { cout<<”Throwing uncaught exception”<<endl; throw excep2(); } catch(excep1) //true if throw excep1 is executed in try scope { //action for exception cout<<”Exception1”; } //excep2 is not caught hence, program aborts here without proceeding further cout<<”I am not displayed”; }
  • 13. 09/04/1313 VIT - SCSE Output: Throwing uncaught exception Program aborted
  • 14. 09/04/1314 VIT - SCSE set_terminate user can define their own exception handler or user defined exception set_terminate is defined in except.h
  • 15. 09/04/1315 VIT - SCSE The program to handle uncaught exceptions with the user specified terminate function //all exceptions are not caught, executes MyTerminate() #include<process.h> #include<except.h> class excep1 { }; class excep2 { }; void MyTerminate() { cout<<”My Terminate is invoked”; exit(1); }
  • 16. 09/04/1316 VIT - SCSE void main() { set_terminate(MyTerminate); //set to our own terminate function try { cout<<”Throwing uncaught exception”<<endl; throw excep2(); } catch(excep1) //true if throw excep1 is executed in try scope { //action for exception cout<<”Caught exception1”; } //program abort() here, MyTransaction will be called cout<<”I am not displayed”; }
  • 17. 09/04/1317 VIT - SCSE Output: Throwing uncaught exception My Terminate is invoked
  • 18. 09/04/1318 VIT - SCSE unexpected() The unexpected () function is called when a function throws an exception not listed in its exception specification. The program calls unexpected() which calls any user-defined function registered by set_unexpected(). If no function is registered with set_unexpected, the unexpected() function then invokes the terminate() function. The prototype of the unexpected() call is void unexpected(); the function unexpected() returns nothing (void). But it can throw an exception through the execution of a function registered by the set_unexpected function.
  • 19. 09/04/1319 VIT - SCSE //unexpected exception #include<except.h> class zero { }; void dis(int num) throw() { if(num>0) cout<<"+ve number"; else if(num<0) cout<<"-ve number"; else throw zero(); //unspecified exception } void main() { int num; cout<<"Enter any number"; cin>>num; try { dis(num); } catch(...) { cout<<"catch all exceptions"; } cout<<endl<<"end of main"; getch(); }
  • 20. 09/04/1320 VIT - SCSE Output 1: Enter any number 10 +ve number end of main Output 2: Enter any number -5 -ve number end of main Output 3: Enter any number 0 Program aborted
  • 21. 09/04/1321 VIT - SCSE set_unexcepted() #include<process.h> //has prototype for exit() #include<except.h> class zero { }; void dis(int num) throw() { if(num>0) cout<<"+ve number"; else if(num<0) cout<<"-ve number"; else throw zero(); //unspecified exception } //this is automatically called whenever an unexpected exception occurs void MyUnexpected() { cout<<”My unexpected handler is invoked”; exit(34); } void main() { int num; cout<<"Enter any number"; cin>>num; set_unexpected(MyUnexpected); //user defined handler
  • 22. 09/04/1322 VIT - SCSE try { dis(num); } catch(...) { cout<<"catch all exceptions"; } cout<<endl<<"end of main"; getch(); } Output 1: Enter any number 10 +ve number end of main Output 2: Enter any number -5 -ve number end of main Output 1: Enter any number 0 My unexpected handler is invoked