Exception
• An exception is a unusual, often
unpredictable event, detectable by
software or hardware, that requires
special processing occurring at runtime
• In C++, a variable or class object that
represents an exceptional event.
Handling Exception
• If without handling,
• Program crashes
• Falls into unknown state
• An exception handler is a section of
program code that is designed to execute
when a particular exception occurs
• Resolve the exception
• Lead to known state, such as exiting
the program
Standard Exceptions
 Exceptions Thrown by the Language
– new
 Exceptions Thrown by Standard
Library Routines
 Exceptions Thrown by user code,
using throw statement
The throw Statement
Throw: to signal the fact that an
exception has occurred; also called
raise
ThrowStatement throw Expression
The try-catch Statement
try
Block
catch (FormalParameter)
Block
catch (FormalParameter)
TryCatchStatement
How one part of the program catches and processes
the exception that another part of the program throws.
FormalParameter
DataType VariableName
…
Execution of try-catch
No
statements throw
an exception
Statement
following entire try-catch
statement
A
statement throws
an exception
Exception
Handler
Statements to deal with exception are executed
Control moves
directly to exception
handler
Example of a try-catch Statement
try
{
// Statements that process personnel data and may throw
// exceptions of type int, string, and SalaryError
}
catch ( int )
{
// Statements to handle an int exception
}
catch ( string s )
{
cout << s << endl; // Prints "Invalid customer age"
// More statements to handle an age error
}
catch ( SalaryError )
{
// Statements to handle a salary error
}
int main()
{ int a,b;
cout<<"Enter the values of a and b n";
cin>>a;
cin>>b;
int x=a-b;
try
{
if(x!=0)
cout<<"Result(a/x) = "<<a/x <<endl;
else
{
throw(x);
}
}catch(int i)
{
cout<<"Exception caught :"<<x<<endl;
}
cout<<"nEND";
return 0;
}
Enter the values of a
and b
10
10
Exception caught :0
END
Enter the values of a
and b
10
5
Result(a/x) = 2
END
int main()
{ int a,b;
cout<<"Enter the values of a and b n";
cin>>a;
cin>>b;
int x=a-b;
try
{ try
{ if(x!=0)
cout<<"Result(a/x) = "<<a/x <<endl;
else
{
throw(x);
}
}catch(int i)
{ cout<<"Exception caught :"<<x<<endl;
throw;
}
} catch(...)
{
cout<<"Caught rethrown exceptionn";
}
cout<<"nEND";
return 0;
}
Enter the values
of a and b
10
10
Exception
caught :0
Caught rethrown
exception
END
Throwing an Exception to be
Caught by the Calling Code
void Func4()
{
if ( error )
throw ErrType();
}
Normal
return
void Func3()
{
try
{
Func4();
}
catch ( ErrType )
{
}
}
Function
call
Return from
thrown
exception
#include <iostream>
using namespace std;
void divide(int x, int y, int z)
{
cout<<"n Inside Functionn";
if((x-y)!=0)
{
int r=z/(x-y);
cout<<"n Result :"<<r<<endl;
}
else
{
throw(x-y);
}
}
int main()
{
cout<<"Inside Main"<<endl;
try
{
divide(10,20,30);
divide(10,10,20);
}
catch(int i)
{
cout<<"Exception
caught :"<<i<<endl;
}
return 0;
}
Inside Main
Inside Function
Result :-3
Inside Function
Exception caught :0
#include <iostream>
using namespace std;
void test(int x)
{
try
{
if(x==1) throw x;
else if(x==0) throw 'x';
else if(x==-1) throw 1.0;
cout<<"nend of try block"<<endl;
}catch(char c){
cout<<"Caught a character"<<endl;
} catch(int m)
{ cout<<"Caught an
integer"<<endl;
} catch(double d)
{ cout<<"Caught a double"<<endl;
} cout<<"End of try-catch segmentn
n";
}
int 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);
return 0;
}
Testing Multiple
catchesx==1
Caught an integer
End of try-catch segment
x==0
Caught a character
End of try-catch segment
x==-1
Caught a double
End of try-catch segment
x==2
end of try block
End of try-catch segment
// quotient.cpp -- Quotient program
#include<iostream.h>
#include <string.h>
int Quotient( int, int );
class DivByZero {}; // Exception class
int main()
{
int numer; // Numerator
int denom; // Denominator
//read in numerator
and denominator
while(cin)
{
try
{
cout << "Their quotient: "
<< Quotient(numer,denom) <<endl;
}
catch ( DivByZero )//exception handler
{
cout<<“Denominator can't be 0"<< endl;
}
// read in numerator and denominator
}
return 0;
}
 An exception is a unusual, often unpredictable event that
requires special processing occurring at runtime
– throw
– try-catch

Lecture7-ExceptionsLecture7-Exceptions_.ppt

  • 1.
    Exception • An exceptionis a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime • In C++, a variable or class object that represents an exceptional event.
  • 2.
    Handling Exception • Ifwithout handling, • Program crashes • Falls into unknown state • An exception handler is a section of program code that is designed to execute when a particular exception occurs • Resolve the exception • Lead to known state, such as exiting the program
  • 3.
    Standard Exceptions  ExceptionsThrown by the Language – new  Exceptions Thrown by Standard Library Routines  Exceptions Thrown by user code, using throw statement
  • 4.
    The throw Statement Throw:to signal the fact that an exception has occurred; also called raise ThrowStatement throw Expression
  • 5.
    The try-catch Statement try Block catch(FormalParameter) Block catch (FormalParameter) TryCatchStatement How one part of the program catches and processes the exception that another part of the program throws. FormalParameter DataType VariableName …
  • 6.
    Execution of try-catch No statementsthrow an exception Statement following entire try-catch statement A statement throws an exception Exception Handler Statements to deal with exception are executed Control moves directly to exception handler
  • 7.
    Example of atry-catch Statement try { // Statements that process personnel data and may throw // exceptions of type int, string, and SalaryError } catch ( int ) { // Statements to handle an int exception } catch ( string s ) { cout << s << endl; // Prints "Invalid customer age" // More statements to handle an age error } catch ( SalaryError ) { // Statements to handle a salary error }
  • 8.
    int main() { inta,b; cout<<"Enter the values of a and b n"; cin>>a; cin>>b; int x=a-b; try { if(x!=0) cout<<"Result(a/x) = "<<a/x <<endl; else { throw(x); } }catch(int i) { cout<<"Exception caught :"<<x<<endl; } cout<<"nEND"; return 0; } Enter the values of a and b 10 10 Exception caught :0 END Enter the values of a and b 10 5 Result(a/x) = 2 END
  • 9.
    int main() { inta,b; cout<<"Enter the values of a and b n"; cin>>a; cin>>b; int x=a-b; try { try { if(x!=0) cout<<"Result(a/x) = "<<a/x <<endl; else { throw(x); } }catch(int i) { cout<<"Exception caught :"<<x<<endl; throw; } } catch(...) { cout<<"Caught rethrown exceptionn"; } cout<<"nEND"; return 0; } Enter the values of a and b 10 10 Exception caught :0 Caught rethrown exception END
  • 10.
    Throwing an Exceptionto be Caught by the Calling Code void Func4() { if ( error ) throw ErrType(); } Normal return void Func3() { try { Func4(); } catch ( ErrType ) { } } Function call Return from thrown exception
  • 11.
    #include <iostream> using namespacestd; void divide(int x, int y, int z) { cout<<"n Inside Functionn"; if((x-y)!=0) { int r=z/(x-y); cout<<"n Result :"<<r<<endl; } else { throw(x-y); } } int main() { cout<<"Inside Main"<<endl; try { divide(10,20,30); divide(10,10,20); } catch(int i) { cout<<"Exception caught :"<<i<<endl; } return 0; } Inside Main Inside Function Result :-3 Inside Function Exception caught :0
  • 12.
    #include <iostream> using namespacestd; void test(int x) { try { if(x==1) throw x; else if(x==0) throw 'x'; else if(x==-1) throw 1.0; cout<<"nend of try block"<<endl; }catch(char c){ cout<<"Caught a character"<<endl; } catch(int m) { cout<<"Caught an integer"<<endl; } catch(double d) { cout<<"Caught a double"<<endl; } cout<<"End of try-catch segmentn n"; } int 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); return 0; }
  • 13.
    Testing Multiple catchesx==1 Caught aninteger End of try-catch segment x==0 Caught a character End of try-catch segment x==-1 Caught a double End of try-catch segment x==2 end of try block End of try-catch segment
  • 14.
    // quotient.cpp --Quotient program #include<iostream.h> #include <string.h> int Quotient( int, int ); class DivByZero {}; // Exception class int main() { int numer; // Numerator int denom; // Denominator //read in numerator and denominator while(cin) { try { cout << "Their quotient: " << Quotient(numer,denom) <<endl; } catch ( DivByZero )//exception handler { cout<<“Denominator can't be 0"<< endl; } // read in numerator and denominator } return 0; }
  • 15.
     An exceptionis a unusual, often unpredictable event that requires special processing occurring at runtime – throw – try-catch