SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a 13 exception handling

Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref. .
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javagopalrajput11
 
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 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
 
SEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitationSEH based buffer overflow vulnerability exploitation
SEH based buffer overflow vulnerability exploitationPayampardaz
 
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 handlingSajedAbir
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in javaManav Prasad
 
Exception handling
Exception handlingException handling
Exception handlingWaqas Abbasi
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 

Semelhante a 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
 
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
 
Exception handling
Exception handlingException handling
Exception handling
 

Mais de Docent Education

Mais de 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
 

Último

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 

Último (20)

How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.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