









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Programming, Object, Orieted, Termination, Abnormal, Error, Handling, Clean, Return, Code, Catch
Typology: Slides
1 / 17
This page cannot be seen from the preview
Don't miss anything!










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; }}
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; }}
► ► 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
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; } }
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
int main() { int main() {
function1(); function1(); function2(); function2(); function3(); function3();
return 0; return 0;
} }
► ► 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
► ► 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 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
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
try {try { function1();function1(); function2();function2(); function3();function3(); }} catch( ErrorX) { ... }catch( ErrorX) { ... } catch( ErrorY) { ... }catch( ErrorY) { ... } catch( ErrorZ) { ... }catch( ErrorZ) { ... } return 0;return 0; }}