Docsity

Abnormal Termination-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Toggle sidebar
bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 43
Lecture No. 43
Techniques for Error Handling
Techniques for Error Handling
►
►
Abnormal termination
Abnormal termination
►
►
Graceful termination
Graceful termination
►
►
Return the illegal value
Return the illegal value
►
►
Return error code from a function
Return error code from a function
►
►
Exception handling
Exception handling
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Abnormal Termination-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

ObjectObject--Oriented ProgrammingOriented Programming

(OOP)(OOP)

Lecture No. 43 Lecture No. 43

Example –Example– Abnormal TerminationAbnormal Termination

void GetNumbers( int &a, int &b ) {void GetNumbers( int &a, int &b ) { cout <<cout << ““\nEnter two integersnEnter two integers””;; cin >> a >> b;cin >> a >> b; }} int Quotient( int a, int b ){int Quotient( int a, int b ){ return a / b;return a / b; }} void OutputQuotient( int a, int b, int quo ) {void OutputQuotient( int a, int b, int quo ) { cout <<cout << ““Quotient ofQuotient of ”” << a <<<< a << ““ andand ”” << b <<<< b << ““ isis ”” << quo << endl;<< quo << endl; }}

ExampleExample –– Graceful TerminationGraceful Termination

int Quotient (int a, int b ) { int Quotient (int a, int b ) { if(b == 0){if(b == 0){ cout << “cout <<“Denominator canDenominator can’’tt ““ << “<<“ be zerobe zero”” << endl;<< endl; // Do local clean up// Do local clean up exit(1);exit(1); }} return a / b;return a / b; }}

Error HandlingError Handling

► ► The clean-The clean-up tasks are of local nature onlyup tasks are of local nature only

► ► There remains the possibility of informationThere remains the possibility of information lossloss

ExampleExample – – Return Error CodeReturn Error Code

bool Quotient ( int a, int b, int & retVal ) { bool Quotient ( int a, int b, int & retVal ) { if(b == 0){ if(b == 0){ return false; return false; } } retVal = a / b; retVal = a / b; return true; return true; } }

OutputOutput

Enter two integers Enter two integers 1010 (^00) Denominator can Denominator can’’t be zero. Give input again.t be zero. Give input again. Enter two integers Enter two integers 1010 1010 Quotient of 10 and 10 is 1 Quotient of 10 and 10 is 1 ...//there will be exactly ten quotients ...//there will be exactly ten quotients

ExampleExample

int main() { int main() {

function1(); function1(); function2(); function2(); function3(); function3();

return 0; return 0;

} }

Exception HandlingException Handling

► ► Exception handling is a much elegantException handling is a much elegant solution as compared to other error solution as compared to other error handling mechanisms handling mechanisms

► ► It enables separation of main logic and errorIt enables separation of main logic and error handling code handling code

ThrowThrow

► ► Primitive data types may be avoided asPrimitive data types may be avoided as throw expression, as they can cause throw expression, as they can cause ambiguity ambiguity

► ► Define new classes to represent theDefine new classes to represent the exceptions that has occurred exceptions that has occurred   (^) This way there are less chances of ambiguityThis way there are less chances of ambiguity

Catch BlocksCatch Blocks

► ► Catch handler must be preceded by a tryCatch handler must be preceded by a try block or an other catch handler block or an other catch handler

► ► Catch handlers are only executed when anCatch handlers are only executed when an exception has occurred exception has occurred

► ► Catch handlers are differentiated on theCatch handlers are differentiated on the basis of argument type basis of argument type

OutputOutput

Enter two integers Enter two integers 1010 (^1010) Quotient of 10 and 10 is 1 Quotient of 10 and 10 is 1 Enter two integers Enter two integers 1010 00 Attempt to divide numerator with zero Attempt to divide numerator with zero ... ... // there will be sum of exactly ten quotients // there will be sum of exactly ten quotients

Separation of Program Logic andSeparation of Program Logic and

int main() {int main() { Error Handling^ Error Handling

try {try { function1();function1(); function2();function2(); function3();function3(); }} catch( ErrorX) { ... }catch( ErrorX) { ... } catch( ErrorY) { ... }catch( ErrorY) { ... } catch( ErrorZ) { ... }catch( ErrorZ) { ... } return 0;return 0; }}