SlideShare a Scribd company logo
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

More Related Content

Similar to 13 exception handling

Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
Rajan Shah
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
. .
 
Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
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
ZenLooper
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
Md. Tanvir Hossain
 
Handling
HandlingHandling
Handling
Amit Vats
 
Unit iii pds
Unit iii pdsUnit iii pds
java.pptx
java.pptxjava.pptx
java.pptx
sujatha629799
 
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
 
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
estorebackupr
 
SEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitationSEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitation
Payampardaz
 
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
 
Exception handling
Exception handlingException handling
Exception handling
SajedAbir
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
Manav Prasad
 
Exception handling
Exception handlingException handling
Exception handling
Waqas Abbasi
 
Exception handling
Exception handlingException handling
Exception handling
zindadili
 

Similar to 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
 

More from Docent Education

17 files and streams
17 files and streams17 files and streams
17 files and streams
Docent Education
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
Docent Education
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
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
Docent Education
 
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
Docent Education
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
Docent Education
 
10 inheritance
10 inheritance10 inheritance
10 inheritance
Docent Education
 
7 class objects
7 class objects7 class objects
7 class objects
Docent Education
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
Docent Education
 
5 array
5 array5 array
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
Docent Education
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented Programming
Docent Education
 
3 intro basic_elements
3 intro basic_elements3 intro basic_elements
3 intro basic_elements
Docent Education
 
2 Intro c++
2 Intro c++2 Intro c++
2 Intro c++
Docent Education
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
Docent Education
 

More from 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
 

Recently uploaded

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 

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