SlideShare une entreprise Scribd logo
1  sur  15
G. H. RAISONI COLLEGE OF ENGINEERING
(AN AUTONOMOUS INSTITUTE AFFILIATED BY R. T. M. NAGPUR UNIVERSITY UNDER UGC
ACT 1956)
DEPARTMENT OF INFORMATION TECHNOLOGY
SESSION 2016-2017
Object Oriented Programming
Through C++
TAE-2
Topic: exception handling in c++
Deepak tathe (37)
WHAT IS AN EXCEPTION ?
 It is often easier to write a program by first assuming that
nothing incorrect will happen
 But sometimes user enters an Invalid value e.g. In a
factorial program if a user enters any negative value.
 For such cases Exception Handling is used.
 Using Exception Handling you can easily manage and
respond to run-time error.
COMMON FAILURES
 new not allocating memory
 Division by zero
 Invalid function parameters
 C does not have Exception Handling Mechanism
 But, C++ have build in Exception Handling
Mechanism
 We can handle exception without exception handling
but that will not be a professional and systematic
approach and it will not clear the program logic.
 In Exception handling we use three keywords try , catch,
throw.
 Program statements you want to monitor for exceptions are
contained in a try box
 If an exception occurs within the try block it is thrown
 The exception is caught using Catch and processed.
Try , Catch , Throw
RELATING EXCEPTION HANDLING WITH REAL LIFE
If a Smartphone
fails the test
try throw
catch
IMPORTANT POINTS
 When a try block end it must be followed by a catch block.
 If we write catch block but don’t write try block then the program
will show
 If we don’t write try and catch but write throw then the program
will build and run but it will show run time error.
#include<iostream>
main()
{
std::cout<<"welcome";
throw 10;
return 0;
}
TRY BLOCK
 In an program a block of code which can produce error
is placed in try block
 When an exception is detected in try it is thrown using
throw statement
 Syntax
try
{
throw exception;
}
catch(type argument)
{
}
#include<iostream>
using namespace std;
main()
{
int a,b;
cout<<"enter values of a and bn";
cin>>a;
cin>>b;
int x=a-b;
try{
if (x!=0)
{
cout<<"result(a/x)="<<a/x<<"n";
}
else{
throw(x);
}
}
catch(int i)
{
cout<<"exception
caught:x="<<x<<"n";
}
cout<<"end";
return 0;
}
THROWING MECHANISM
 An exception is thrown using the throw statement in one of the following
ways
throw(exception);
throw exception;
throw;
o We can add use multiple throw statements by using if – else
statement
e.g.
int i=3;
Try {
if(i==1)
throw 1;
if(i==2)
throw 2;
}
CALL THROW USING FUNCTION
#include<iostream>
using namespace std;
void fun()
{
throw 3;
}
int main()
{
int i=3;
try{
if (i=3)
fun();
}
catch(int e){
cout<<"n exception no:"<<e;
}
cout<<"n throw successfully called by
function";
getch();
}
fun function
contains throw
In try instead of writing throw
fun function is calling throw
CATCHING MECHANISM
 A catch block look like a function definition and is of the form
Catch (type arg)
{
// statements for managing exceptions
}
 The type indicates which type of exception that catch block handles
 The catch statement catches an exception whose type matches with the
type of catch argument. When it is caught the code in the catch block is
executed
 Due to mismatched, if an exception is not caught, abnormal program
termination will occur
 The catch block is simply skipped if the catch statement does not catch
an exception
MULTIPLE CATCH STATEMENT
 Syntax for multiple catch statement
MULTIPLE CATCH
 In multiple catch when an exception is thrown the
exception handlers are searched in order of an
appropriate match
 When no match is found the program is terminated
 It is possible that arguments of several catch
statement match the type of an exception in such
cases the first handler that matches the exception
is executed
CATCH ALL EXCEPTION
 One catch statement can catch all exception
instead of a certain type alone.
 This could be done by defining the catch
statements using ellipse as follows
catch (…)
{
// statements for processing
// all exceptions
}
Exception Handling in C++

Contenu connexe

Tendances

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 

Tendances (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Files in c++
Files in c++Files in c++
Files in c++
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
class and objects
class and objectsclass and objects
class and objects
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
File in C language
File in C languageFile in C language
File in C language
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Function in C program
Function in C programFunction in C program
Function in C program
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 

En vedette

Exception Handling
Exception HandlingException Handling
Exception Handling
Alpesh Oza
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
jigeno
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
Fahim Adil
 
Report on GPU complex type usage
Report on GPU complex type usageReport on GPU complex type usage
Report on GPU complex type usage
Caner Candan
 

En vedette (20)

Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Exception handling
Exception handlingException handling
Exception handling
 
C++ ala
C++ alaC++ ala
C++ ala
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
14 exception handling
14 exception handling14 exception handling
14 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
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
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Report on GPU complex type usage
Report on GPU complex type usageReport on GPU complex type usage
Report on GPU complex type usage
 

Similaire à Exception Handling in C++

Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 

Similaire à Exception Handling in C++ (20)

$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Exception handling
Exception handlingException handling
Exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Savitch ch 16
Savitch ch 16Savitch ch 16
Savitch ch 16
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exceptions in c++
Exceptions in c++Exceptions in c++
Exceptions in c++
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Savitch Ch 16
Savitch Ch 16Savitch Ch 16
Savitch Ch 16
 
Exception handling
Exception handlingException handling
Exception handling
 

Dernier

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 

Exception Handling in C++

  • 1. G. H. RAISONI COLLEGE OF ENGINEERING (AN AUTONOMOUS INSTITUTE AFFILIATED BY R. T. M. NAGPUR UNIVERSITY UNDER UGC ACT 1956) DEPARTMENT OF INFORMATION TECHNOLOGY SESSION 2016-2017 Object Oriented Programming Through C++ TAE-2 Topic: exception handling in c++ Deepak tathe (37)
  • 2. WHAT IS AN EXCEPTION ?  It is often easier to write a program by first assuming that nothing incorrect will happen  But sometimes user enters an Invalid value e.g. In a factorial program if a user enters any negative value.  For such cases Exception Handling is used.  Using Exception Handling you can easily manage and respond to run-time error.
  • 3. COMMON FAILURES  new not allocating memory  Division by zero  Invalid function parameters  C does not have Exception Handling Mechanism  But, C++ have build in Exception Handling Mechanism  We can handle exception without exception handling but that will not be a professional and systematic approach and it will not clear the program logic.
  • 4.  In Exception handling we use three keywords try , catch, throw.  Program statements you want to monitor for exceptions are contained in a try box  If an exception occurs within the try block it is thrown  The exception is caught using Catch and processed. Try , Catch , Throw
  • 5. RELATING EXCEPTION HANDLING WITH REAL LIFE If a Smartphone fails the test try throw catch
  • 6. IMPORTANT POINTS  When a try block end it must be followed by a catch block.  If we write catch block but don’t write try block then the program will show  If we don’t write try and catch but write throw then the program will build and run but it will show run time error. #include<iostream> main() { std::cout<<"welcome"; throw 10; return 0; }
  • 7. TRY BLOCK  In an program a block of code which can produce error is placed in try block  When an exception is detected in try it is thrown using throw statement  Syntax try { throw exception; } catch(type argument) { }
  • 8. #include<iostream> using namespace std; main() { int a,b; cout<<"enter values of a and bn"; cin>>a; cin>>b; int x=a-b; try{ if (x!=0) { cout<<"result(a/x)="<<a/x<<"n"; } else{ throw(x); } } catch(int i) { cout<<"exception caught:x="<<x<<"n"; } cout<<"end"; return 0; }
  • 9. THROWING MECHANISM  An exception is thrown using the throw statement in one of the following ways throw(exception); throw exception; throw; o We can add use multiple throw statements by using if – else statement e.g. int i=3; Try { if(i==1) throw 1; if(i==2) throw 2; }
  • 10. CALL THROW USING FUNCTION #include<iostream> using namespace std; void fun() { throw 3; } int main() { int i=3; try{ if (i=3) fun(); } catch(int e){ cout<<"n exception no:"<<e; } cout<<"n throw successfully called by function"; getch(); } fun function contains throw In try instead of writing throw fun function is calling throw
  • 11. CATCHING MECHANISM  A catch block look like a function definition and is of the form Catch (type arg) { // statements for managing exceptions }  The type indicates which type of exception that catch block handles  The catch statement catches an exception whose type matches with the type of catch argument. When it is caught the code in the catch block is executed  Due to mismatched, if an exception is not caught, abnormal program termination will occur  The catch block is simply skipped if the catch statement does not catch an exception
  • 12. MULTIPLE CATCH STATEMENT  Syntax for multiple catch statement
  • 13. MULTIPLE CATCH  In multiple catch when an exception is thrown the exception handlers are searched in order of an appropriate match  When no match is found the program is terminated  It is possible that arguments of several catch statement match the type of an exception in such cases the first handler that matches the exception is executed
  • 14. CATCH ALL EXCEPTION  One catch statement can catch all exception instead of a certain type alone.  This could be done by defining the catch statements using ellipse as follows catch (…) { // statements for processing // all exceptions }