2. What is an Exception ?
1 .When a program is executed, unexpected situation may
occur. Such a situation is called an exception.
EG : a) indexing outside the limits in an array,
b) giving faulty input data
c) failure of new to obtain a requested amount of
memory.
2. An exception is not necessarily the result of a logic
error in the program. It also can arise from faulty input
data.
2/15
3. Example
Index out of range
Division by zero
int array [10];
float x, y;
for (int i=0; i<=10; i++)
….
array [i] = something;
y = 0.0;
float result = x/y
No Space
class Student
{...}
int main ()
{Student *aStudent;
aStudent = new Student (...);
...}
3/15
4. To enable the program to take care (handle)of such
exceptional situations, C++ provides the following features:
1. Try Block
- The code which might generate a runtime error is
written within the try block.
2. Throw
The programmer can generate an exception using throw
3. Catch Block
Catches the error which may be generated from the code
within the try block.
A try block should be followed by one or more catch blocks.
General Format - Next Slide
4/15
5. General Format
Try {
c++ valid statements;
}
catch( )
{ error handling part;
}
catch(argument )
{ error handling part;
}
5/15
6. # include <iostream.h> We write the code
int main() in a try block
{ int value1, value2, result;
try
{ cin >> value1; cin >> value2; If there is an exception,
we throw it to a handler
if (value2 == 0)
{ throw ; } If there is no exception,
we resume the execution
result = value1/value2;
cout <<"result is :"<< result;
}// end of try
catch ( )
{ cout << " just cannot divide by zero";
}// end of catch
6/15
}// end of main
7. Some times, we might have many different exceptions
1. We should write as many catch blocks.
2. This means also that we should have as many
throw statements.
3. BUT(usually), only one try.
But, which catch block will be instigated? (invoked)
The conflict will be eliminated depending on the parameters
in the throw, i.e., OVERLOADING
7/15
8. int main()
{ int value1, value2, result; catch ( )
{cout << " just cannot divide
by zero";
try }// end of catch
{cin >> value1;cin >> value2;
catch (int v )
if (value1 < 0)
{cout << v << "is less than zero,
{throw (value1);
can’t you see?";
}
}// end of catch
if (value2 == 0)
{throw ; …
} return 0;
result = value1/value2; }// end of main
cout <<"result is :"<< result;
}// end of try
8/15
9. Example
Int main ( ) will this CATCH work ?
{ try{ Int main ( )
cout<<“inside try”; { try{
throw 100; cout<<“inside try”;
cout<<“will this execute”; throw 100;
} cout<<“will this
execute”;
}
catch(int I) {
cout <<“the caught an
exception of value”<<I; } catch(double I) {
} cout <<“the caught an
exception of value”<<I; }
9/15
}
12. CD 1 class CDtorDemo
CD 2 { public:
CD 3 CDtorDemo();
CD 4 ~CDtorDemo();
CD 5 };
CD 6 CDtorDemo::CDtorDemo()
CD 7 {cout << "Constructing CDtorDemo." << endl;}
CD 8 CDtorDemo::~CDtorDemo()
CD 9 {cout << "Destructing CDtorDemo." << endl;}
My 1 void MyFunc()
My 2 { CDtorDemo D;
My 3 cout<< "In MyFunc(). Throwing CTest exception."
<< endl;
My 4 throw CTest();
My 5 } 12/15
13. int main()
{
1 cout << "In main." << endl;
2 try
3 { cout << "In try block, calling MyFunc()."<<endl;
4 MyFunc();
5 } // end try
6 catch( CTest E )
7 { cout << "In catch handler." << endl;
8 cout << "Caught CTest exception type: ";
9 cout << E.ShowReason() << endl;
10 } //end catch( CTest E )
11 catch( char *str )
12 {cout << "Caught some other exception: " << str << endl; }
13 cout << "Back in main. Execution resumes here." << endl;
14 return 0;
15 }// end main()
13/15