exception handling. 2 two types of bugs (errors) logical error syntactic error logical error occur ...

17
Exception Handling popo

Upload: jevon-ough

Post on 16-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

Exception Handling

popo

Page 2: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 3: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 4: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 5: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 6: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

6

Exception HandlingException handling code consists of the following

segments To detect errorTo throw errorTo catch errorTo take appropriate action

popo

Page 7: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 8: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 9: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 10: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 11: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 12: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 13: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 14: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

14

Exception Handling

popo

Page 15: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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.

Page 16: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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

Page 17: Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution

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