SlideShare a Scribd company logo
OBJECT ORIENTED PROGRAMMING USING C++
Created By:
Kumar Vishal
(SCA), LPU
CAP444
OBJECT ORIENTED PROGRAMMING
USING C++
Unit6
OBJECT ORIENTED PROGRAMMING USING C++
Unit-6
Exception handling :
 principles of exception handling,
 exception handling mechanism,
 Multiple catch statements,
 catching multiple exceptions,
 re-throwing exceptions,
 exceptions in constructors and destructors,
 controlling uncaught exceptions
OBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++
Exception handling
Exception is an abnormal condition.
Exception is an event that disturbs the normal
flow of the program.
Result out of expectation
OBJECT ORIENTED PROGRAMMING USING C++
Exception Handling Keywords:
• Try : The try block contain statements which
may generate exceptions.
• Throw : When an exception occur in try block,
it is thrown to the catch block using throw
keyword.
• Catch :The catch block defines the action to be
taken, when an exception occur.
OBJECT ORIENTED PROGRAMMING USING C++
Syntax:
try {
// Block of code to try
throw exception; // Throw an
exception when a problem arise
}
catch () {
// Block of code to handle
exception
}
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome"<<endl;
try{
throw 10;
}
catch(int x)
{
cout<<x<<endl;
}
cout<<"End"<<endl;
}
Example
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome"<<endl;
try{
throw 10;
}
catch(double x)
{
cout<<x<<endl;
}
cout<<"End"<<endl;
}
Example
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome"<<endl;
try{
throw 10;
}
catch(double x)
{
cout<<x<<endl;
}
catch(int x)
{
cout<<x<<endl;
}
cout<<"End"<<endl;
}
Example
OBJECT ORIENTED PROGRAMMING USING C++
Example of exception handling for divide by zero
exception:
https://github.com/vishalamc/cap444/blob/mai
n/exceptionHandlingEx
OBJECT ORIENTED PROGRAMMING USING C++
Standard error stream (cerr): cerr is the standard
error stream which is used to output the errors.
It is an instance of the ostream class.
OBJECT ORIENTED PROGRAMMING USING C++
multiple catch statements:
A single try statement can have multiple catch
statements. Execution of particular catch block
depends on the type of exception thrown by the throw
keyword. If throw keyword send exception of integer
type, catch block with integer parameter will get
execute.
Example:
https://github.com/vishalamc/cap444/blob/main/multi
pleCatchStatement
OBJECT ORIENTED PROGRAMMING USING C++
multiple catch statements:
try
{
throw value
}
catch(type1 arg)
{
//catch block1
}
catch(type2 arg)
{
//catch block2
}
………
……… . If throw keyword send exception
of integer type, catch block with integer parameter will
get execute.
OBJECT ORIENTED PROGRAMMING USING C++
Which of the following statements are correct?
A. The execution of a throw statement is called
throwing an exception.
B. You can throw a value of any type.
C. The catch block contains the code that are
executed when an exception occurs.
D. All of the above
OBJECT ORIENTED PROGRAMMING USING C++
Catch All Exceptions
We can use the catch statement with three dots as
parameter (...) so that it can handle all types of
exceptions.
catch(…)
{
}
Note: The catch(...) must be the last catch
block.
Example:
https://github.com/vishalamc/cap444/blob/main/catchAllT
ypesException
OBJECT ORIENTED PROGRAMMING USING C++
Rethrowing Exceptions
• If a catch block cannot handle the particular
exception it has caught, you can rethrow the
exception.
• Rethrowing exception can possible in case of
Nested try-catch statement. An exception to
be thrown from inner catch block to outer
catch block is also called rethrowing
exception.
OBJECT ORIENTED PROGRAMMING USING C++
Example1:( Rethrowing exception in nested try block)
https://github.com/vishalamc/cap444/blob/main/exce
ptionRethrowingNestedTryExample
Example2: (Rethrowing exception in function)
https://github.com/vishalamc/cap444/blob/main/rethr
owingExceptionExample
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
cout << "Enter two integers: ";
int number1, number2;
cin >> number1 >> number2;
try
{
if (number2 == 0)
throw number1;
cout << number1 << " / " << number2 <<
" is "
<< (number1 / number2) << endl;
cout << "C" << endl;
}
catch (int e)
{
cout<< "A";
} cout << "B"; return 0;
}
If you enter 1 0, what is the
output of the following code?
Options:
A.A
B. B
C. C
D.AB
OBJECT ORIENTED PROGRAMMING USING C++
Exceptions in Constructors and Destructors
• If an exception is raised in a constructor, memory
might be allocated to some data members and might
not be allocated for others. This might lead to
memory leakage problem.
• Similarly, when an exception is raised in a destructor,
memory might not be deallocated which may again
lead to memory leakage problem.
• So, it is better to provide exception handling within
the constructor and destructor to avoid such
problems.
OBJECT ORIENTED PROGRAMMING USING C++
Syntax:
class division
{
division()
{
try{
}
catch()
{
}
}
};
~division()
{
try{
}
catch()
{
}
}
Example:
https://github.com/vishalamc/cap444/blob/main/exceptionInConstructorDestructorExa
mple
OBJECT ORIENTED PROGRAMMING USING C++
If inner catch handler is not able to handle the
exception then__________ .
A. Compiler will look for outer try handler
B. Program terminates abnormally
C. Compiler will check for appropriate catch
handler of outer try block
D. None of the above
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
try
{
throw 'b';
}
catch (int param)
{
cout << "Int Exception";
}
catch (...)
{
cout << "Default Exception";
}
cout << "After Exception";
return 0;
}
A. Default Exception After Exception
B. Int Exception After Exception
C. Int Exception
D. Default Exception
What will be output?
OBJECT ORIENTED PROGRAMMING USING C++
Uncaught exceptions
In some case when an exception is thrown
but isn’t caught because the exception
handling subsystem fails to find a matching
catch block for that particular exception.
If there’s no exception handler then main()
terminates.
OBJECT ORIENTED PROGRAMMING USING C++
Example:
#include <iostream>
using namespace std;
int main()
{
int x=5;
try{
throw 0;
}
catch(char c)
{
cout<<"exception";
}
return 0;
}
OBJECT ORIENTED PROGRAMMING USING C++
What function will be called when we have a uncaught exception?
A. catch
B. throw
C. terminate
D. none of the mentioned
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
try
{
throw 10;
}
catch (...)
{
cout << "Default Exceptionn";
}
catch (int param)
{
cout << "Int Exceptionn";
}
return 0;
}
A. Default Exception
B. Int Exception
C. Compiler Error
D. None of the above
OBJECT ORIENTED PROGRAMMING USING C++
#include <iostream>
using namespace std;
int main()
{
int P = -1;
try {
cout << "Inside try";
if (P < 0)
{
throw P;
cout << "After throw";
}
}
catch (int P ) {
cout << "Exception Caught";
}
cout << "After catch";
return 0;
}
A. Inside try Exception Caught After
throw After catch
B. Inside try Exception Caught After catch
C. Inside try Exception Caught
D. Inside try After throw After catch
OBJECT ORIENTED PROGRAMMING USING C++
Any Query?
Unit-6 End

More Related Content

Similar to CAP444Unit6ExceptionHandling.pptx

exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
SulekhJangra
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
Rajan Shah
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxWINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16
Max Kleiner
 
Unit iii pds
Unit iii pdsUnit iii pds
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
People Strategists
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
Surajgroupsvideo
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
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
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
Rapita Systems Ltd
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
mcollison
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
Open Gurukul
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
JAYAPRIYAR7
 
Exception handling
Exception handlingException handling
Exception handling
Waqas Abbasi
 
43c
43c43c
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
gourav kottawar
 

Similar to CAP444Unit6ExceptionHandling.pptx (20)

exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxWINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
 
A exception ekon16
A exception ekon16A exception ekon16
A exception ekon16
 
Unit iii pds
Unit iii pdsUnit iii pds
Unit iii pds
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
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++
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
Exception handling
Exception handlingException handling
Exception handling
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
 
Exception handling
Exception handlingException handling
Exception handling
 
43c
43c43c
43c
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 

Recently uploaded

"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
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
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
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
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 

Recently uploaded (20)

"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
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
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
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
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 

CAP444Unit6ExceptionHandling.pptx

  • 1. OBJECT ORIENTED PROGRAMMING USING C++ Created By: Kumar Vishal (SCA), LPU CAP444 OBJECT ORIENTED PROGRAMMING USING C++ Unit6
  • 2. OBJECT ORIENTED PROGRAMMING USING C++ Unit-6 Exception handling :  principles of exception handling,  exception handling mechanism,  Multiple catch statements,  catching multiple exceptions,  re-throwing exceptions,  exceptions in constructors and destructors,  controlling uncaught exceptions
  • 4. OBJECT ORIENTED PROGRAMMING USING C++ Exception handling Exception is an abnormal condition. Exception is an event that disturbs the normal flow of the program. Result out of expectation
  • 5. OBJECT ORIENTED PROGRAMMING USING C++ Exception Handling Keywords: • Try : The try block contain statements which may generate exceptions. • Throw : When an exception occur in try block, it is thrown to the catch block using throw keyword. • Catch :The catch block defines the action to be taken, when an exception occur.
  • 6. OBJECT ORIENTED PROGRAMMING USING C++ Syntax: try { // Block of code to try throw exception; // Throw an exception when a problem arise } catch () { // Block of code to handle exception }
  • 7. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { cout<<"Welcome"<<endl; try{ throw 10; } catch(int x) { cout<<x<<endl; } cout<<"End"<<endl; } Example
  • 8. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { cout<<"Welcome"<<endl; try{ throw 10; } catch(double x) { cout<<x<<endl; } cout<<"End"<<endl; } Example
  • 9. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { cout<<"Welcome"<<endl; try{ throw 10; } catch(double x) { cout<<x<<endl; } catch(int x) { cout<<x<<endl; } cout<<"End"<<endl; } Example
  • 10. OBJECT ORIENTED PROGRAMMING USING C++ Example of exception handling for divide by zero exception: https://github.com/vishalamc/cap444/blob/mai n/exceptionHandlingEx
  • 11. OBJECT ORIENTED PROGRAMMING USING C++ Standard error stream (cerr): cerr is the standard error stream which is used to output the errors. It is an instance of the ostream class.
  • 12. OBJECT ORIENTED PROGRAMMING USING C++ multiple catch statements: A single try statement can have multiple catch statements. Execution of particular catch block depends on the type of exception thrown by the throw keyword. If throw keyword send exception of integer type, catch block with integer parameter will get execute. Example: https://github.com/vishalamc/cap444/blob/main/multi pleCatchStatement
  • 13. OBJECT ORIENTED PROGRAMMING USING C++ multiple catch statements: try { throw value } catch(type1 arg) { //catch block1 } catch(type2 arg) { //catch block2 } ……… ……… . If throw keyword send exception of integer type, catch block with integer parameter will get execute.
  • 14. OBJECT ORIENTED PROGRAMMING USING C++ Which of the following statements are correct? A. The execution of a throw statement is called throwing an exception. B. You can throw a value of any type. C. The catch block contains the code that are executed when an exception occurs. D. All of the above
  • 15. OBJECT ORIENTED PROGRAMMING USING C++ Catch All Exceptions We can use the catch statement with three dots as parameter (...) so that it can handle all types of exceptions. catch(…) { } Note: The catch(...) must be the last catch block. Example: https://github.com/vishalamc/cap444/blob/main/catchAllT ypesException
  • 16. OBJECT ORIENTED PROGRAMMING USING C++ Rethrowing Exceptions • If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. • Rethrowing exception can possible in case of Nested try-catch statement. An exception to be thrown from inner catch block to outer catch block is also called rethrowing exception.
  • 17. OBJECT ORIENTED PROGRAMMING USING C++ Example1:( Rethrowing exception in nested try block) https://github.com/vishalamc/cap444/blob/main/exce ptionRethrowingNestedTryExample Example2: (Rethrowing exception in function) https://github.com/vishalamc/cap444/blob/main/rethr owingExceptionExample
  • 18. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { cout << "Enter two integers: "; int number1, number2; cin >> number1 >> number2; try { if (number2 == 0) throw number1; cout << number1 << " / " << number2 << " is " << (number1 / number2) << endl; cout << "C" << endl; } catch (int e) { cout<< "A"; } cout << "B"; return 0; } If you enter 1 0, what is the output of the following code? Options: A.A B. B C. C D.AB
  • 19. OBJECT ORIENTED PROGRAMMING USING C++ Exceptions in Constructors and Destructors • If an exception is raised in a constructor, memory might be allocated to some data members and might not be allocated for others. This might lead to memory leakage problem. • Similarly, when an exception is raised in a destructor, memory might not be deallocated which may again lead to memory leakage problem. • So, it is better to provide exception handling within the constructor and destructor to avoid such problems.
  • 20. OBJECT ORIENTED PROGRAMMING USING C++ Syntax: class division { division() { try{ } catch() { } } }; ~division() { try{ } catch() { } } Example: https://github.com/vishalamc/cap444/blob/main/exceptionInConstructorDestructorExa mple
  • 21. OBJECT ORIENTED PROGRAMMING USING C++ If inner catch handler is not able to handle the exception then__________ . A. Compiler will look for outer try handler B. Program terminates abnormally C. Compiler will check for appropriate catch handler of outer try block D. None of the above
  • 22. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { try { throw 'b'; } catch (int param) { cout << "Int Exception"; } catch (...) { cout << "Default Exception"; } cout << "After Exception"; return 0; } A. Default Exception After Exception B. Int Exception After Exception C. Int Exception D. Default Exception What will be output?
  • 23. OBJECT ORIENTED PROGRAMMING USING C++ Uncaught exceptions In some case when an exception is thrown but isn’t caught because the exception handling subsystem fails to find a matching catch block for that particular exception. If there’s no exception handler then main() terminates.
  • 24. OBJECT ORIENTED PROGRAMMING USING C++ Example: #include <iostream> using namespace std; int main() { int x=5; try{ throw 0; } catch(char c) { cout<<"exception"; } return 0; }
  • 25. OBJECT ORIENTED PROGRAMMING USING C++ What function will be called when we have a uncaught exception? A. catch B. throw C. terminate D. none of the mentioned
  • 26. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { try { throw 10; } catch (...) { cout << "Default Exceptionn"; } catch (int param) { cout << "Int Exceptionn"; } return 0; } A. Default Exception B. Int Exception C. Compiler Error D. None of the above
  • 27. OBJECT ORIENTED PROGRAMMING USING C++ #include <iostream> using namespace std; int main() { int P = -1; try { cout << "Inside try"; if (P < 0) { throw P; cout << "After throw"; } } catch (int P ) { cout << "Exception Caught"; } cout << "After catch"; return 0; } A. Inside try Exception Caught After throw After catch B. Inside try Exception Caught After catch C. Inside try Exception Caught D. Inside try After throw After catch
  • 28. OBJECT ORIENTED PROGRAMMING USING C++ Any Query? Unit-6 End