KEMBAR78
Exception Handling in C++ | PPTX
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++

Exception Handling in C++

  • 1.
    G. H. RAISONICOLLEGE 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 ANEXCEPTION ?  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  newnot 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 Exceptionhandling 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 HANDLINGWITH REAL LIFE If a Smartphone fails the test try throw catch
  • 6.
    IMPORTANT POINTS  Whena 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  Inan 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() { inta,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  Anexception 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 USINGFUNCTION #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  Acatch 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  Inmultiple 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 }