Professional Documents
Culture Documents
(CS2133)
By
Exceptions
Indicate problems that occur during a program’s execution
Occur infrequently
Exception handling
Can resolve exceptions
Allow a program to continue executing or
Notify the user of the problem and
Terminate the program in a controlled manner
In order to deal with the exceptional situation you throw the exception. Th
is passes control, as well as the exception, to a designated block of code in
a direct or indirect caller of the function that threw the exception. This blo
ck of code is called a handler.
void main()
{
int num1, num2;
cout<<“Enter two integers:”;
cin>>num1>>num2;
}
Exception – An Example
void main()
{
int num1, num2;
cout<<“Enter two integers:”;
cin>>num1>>num2;
if(num2 != 0)
cout<<num1<<“ / ”<<num2<< “ is “<< (num1/num2)<<endl;
else
cout<<“Divisor can not be zero \n”;
}
Exception – An Example
void main()
{
int num1, num2;
cout<<“Enter two integers:”;
cin>>num1>>num2;
try{
if(num2 == 0)
throw num1;
}catch(int e){
cout<<“Exception: an integer “<<e<<“cannot be divided by zero”<<endl;
}
Template for try-throw-catch
try
{
Code to try;
Throw an exception with a throw statement or from function if necessary;
More code to try;
}
catch (type e)
{
Code to process the exception;
}
Omitting catch block parameter
If you are not interested in the contents of an exception object, th
e catch block parameter may be omitted. For example, the followi
ng catch block is legal.
try
{
// ...
}
catch (type)
{
cout << "Error occurred " << endl;
}
Exception Handling Advantages
Exception handling is the process of responding to the occu
rrence anomalous or exceptional situations – requiring spec
ial processing – often changing the normal flow of program
execution.
overflow_error
runtime_error
underflow_error
bad_alloc
bad_cast
exception
bad_type_id
bad_exception
invalid_argument
logic_error length_error
out_of_range
Use of standard Exception classes – example
void main()
{
try{
for(int i=1; i<=100; i++)
{
new int[7000000];
cout<<i<<“ arrays have been created”;
}
}
catch(bad_alloc &e)
{
cout<<“Exception: “<<e.what()<<endl;
}
}
Use of standard Exception classes – example
void main()
{
try{
Rectangle rec(3, 4);
Circle &c = dynamic_cast<Circle &>rec;
}
catch(bad_cast &e)
{
cout<<“Exception: “<<e.what()<<endl;
}
}
Use of standard Exception classes – example
class A {
virtual void Member() = 0;
};
void main ()
{
try{
A *a = 0;
cout << typeid(*a).name();
}
catch (bad_typeid& bt){
cerr << bt.what() << endl;
}
}
Exception Handling with Classes
Exception Handling with Classes
class AClass
{
public:
class AnError{ };
void Func()
{
throw AnError();
}
};
void main()
{
try{
AClass obj1;
obj1.Func();
}catch(AClass::AnError){
cout<<"Error Occurred\n";
}
}
Exception Handling with classes
const int MAX = 3; //stack holds 3 integers void main()
class Stack{ {
private: Stack s1;
int st[MAX]; //array of integers try
int top; //index of top of stack {
public: s1.push(11);
class Range //exception class for Stack s1.push(22);
{ }; s1.push(33);
Stack(){ top = -1; } // s1.push(44);
void push(int var){ cout << "1: " << s1.pop() << endl;
if(top >= MAX-1) //if stack full, cout << "2: " << s1.pop() << endl;
throw Range(); //throw exception cout << "3: " << s1.pop() << endl;
st[++top] = var; //put number on stack cout << "4: " << s1.pop() << endl;
} }
int pop(){ catch(Stack::Range)
if(top < 0) //if stack empty, { cout << "Exception: Stack Full/Empty\n"; }
throw Range(); //throw exception
return st[top--]; //take number off stack cout << "Arrive here after catch";
} }
};
Exception Handling with classes
const int MAX = 3; //stack holds 3 integers void main()
class Stack{ {
private: Stack s1;
int st[MAX]; //array of integers try
int top; //index of top of stack {
public: s1.push(11);
class Full { }; s1.push(22);
class Empty{ }; s1.push(33);
Stack(){ top = -1; } // s1.push(44); //oops: stack full
void push(int var){ cout << "1: " << s1.pop() << endl;
if(top >= MAX-1) //if stack full, cout << "2: " << s1.pop() << endl;
throw Full(); //throw exception cout << "3: " << s1.pop() << endl;
st[++top] = var; //put number on stack cout << "4: " << s1.pop() << endl;
} }
int pop(){ catch(Stack::Full)
if(top < 0) //if stack empty, { cout << "Exception: Stack Full"; }
throw Empty(); //throw exception catch(Stack::Empty)
return st[top--]; //take number off stack { cout << "Exception: Stack Empty"; }
} }
};
Exception Handling with classes
try try
{ {
... ...
} }
catch (exception &ex) catch (logic_error &ex)
{ {
... ...
} }
catch (logic_error &ex) catch (exception &ex)
{ {
... ...
} }
Call Stack
function3
function2 function2
(a) (b)
Exercise
Define a custom exception class named SafeArray and let the pus
h function of this class to throw BoundException (your own defin
ed exception class) if the array is already full. Write a test progra
m with a try-catch block to handle this type of exception.