Upload
jevon-ough
View
217
Download
1
Embed Size (px)
Citation preview
Exception Handling
popo
2
Exception HandlingTwo types of bugs (errors)Logical errorSyntactic error
Logical error occur due to poor understanding of the problem and solution procedure
Syntactic error occur due to poor understanding of language , can solve by compiler
popo
3
Exception HandlingExceptions are runtime errorsSuch as division by zero, array out of bounds, no
disk space, trying to read/write data from improper address (or an unopened file)…
When a run time error encountered the program terminates
C++ provides built in features to detect & handle exceptions
popo
4
Exception HandlingExceptions are 2 typesSynchronous exceptionsAsynchronous exceptions
Synchronous out of range index, overflow, divide by zero..
Asynchronous beyond the control of the program such as keyboard problem….
Exception handling mechanism mainly for Synchronous errors
popo
5
Exception HandlingException handling mechanism detect and report
an runtime error, so appropriate action can be taken
Exception handling mechanism steps1) Find an error (hit exception)2) Inform that an error occurred (throw exception)3) Receive the error information (catch exception)4) Take corrective action(handle exception)
popo
6
Exception HandlingException handling code consists of the following
segments To detect errorTo throw errorTo catch errorTo take appropriate action
popo
7
Exception HandlingException handling mechanism done with three
keywordstrythrowcatchtry blockA block of statements surrounded with braces, which
may generate errorIf you suspect a statement or a set of statements may
cause any runtime error, group them in a block and name it as try
popo
8
Exception Handlingtry{
statements;}throwWhen an error is detected in the try block it is thrown using a throw statement in the
try blocktry{
int a,b;……if(a==0){
throw “Divide by Zero”;}cout<<“checking”;
}
popo
9
Exception Handling• main()• {• cout << "Start\n";• try { // start a try block• cout << "Inside try block\n";• throw (100); // throw an error• cout << "This will not execute";• }• catch (int i) { // catch an error• cout << "Caught an exception -- value is: ";• cout << i << "\n";• }• cout << "End“;• This program displays the following output:• Start• Inside try block• Caught an exception -- value is: 100• End
popo
10
Exception HandlingcatchDefined by the keyword catch Catches the exception thrown by the throw statement in the try blockThe catch block that catches an exception must immediately follow the try blockAny thrown exception must be caught by a catch statement that has thrown the exceptiontry{
int a,b;……if(a==0){throw “Divide by Zero”;}cout<<“checking”;
}catch (type arguments){
statments}
popo
11
Exception HandlingWhen try block throws an exception the program
control leaves the try block and enteres the catch statements
Exceptions are objects used to transmit information about a problem
If the type object throw matches the argument type in the catch , then catch block is executed for handling the error
If they do not matches program terminates
popo
12
Exception HandlingVoid main(){
int a,b,c;cout<<“enter 2 nos “;cin>>a>>b;try{
if(b!=0){
c=a/b;cout<<c;}else{
throw(b); //throw int object}
}catch(int x) // catches the exception{
cout<<“exception caught “ <<x;}
Cout<<“END”;}
popo
13
Exception HandlingException handling in functionVoid check(int x,int y){
if(y!=0){
int z=x/y;cout<<z;}else{
throw(y);}
}Void main(){
try{
check(3,0);}catch(int z){
cout<<“Caught the exception”;}cout<<“END”getch();
}
popo
14
Exception Handling
popo
15
Exception Handling• Using Multiple catch Statements• As stated, you can have more than one catch associated with a try. • In fact, it is common to do so. • However, each catch must catch a different type of exception. • For example, this program catches both integers and strings.• try• {
if(test) throw test;else throw "Value is zero";
• }• catch(int i) • {
cout << "Caught Exception #: " << i << '\n';• }• catch(const char *str) • {
cout << "Caught a string: ";cout << str << '\n';
• }• Each catch statement responds only to its own type.
16
Exception Handling• Catching All Exceptions• In some circumstances you will want an exception
handler to catch all exceptions• This is easy to accomplish. • Simply use this form of catch.• catch(...) • {
// process all exceptions• }
popo
17
Exception Handling• try• {
if(test==0) throw test; // throw intif(test==1) throw 'a'; // throw charif(test==2) throw 123.23; // throw double
• }• catch(...) { // catch all exceptions• cout << "Caught One!\n";• }
popo