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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Stack Unwinding, Flow control of throw, Return statement, Calling function, Nested function calls, Function abort, Nested try blocks, Catch Handler, Body of main Function are main points of this lecture.
Typology: Slides
1 / 26
Object-Oriented Programming (OOP) Lecture No. 44
class DivideByZero { public: DivideByZero() { } }; int Quotient(int a, int b){ if(b == 0){ throw DivideByZero(); } return a / b; }
int main() { try{ … quot = Quotient(a,b); … } catch(DivideByZero) { … } return 0; }
void function1() { ... throw Exception(); … } void function2() {… function1();… } int main() { try{ function2(); } catch( Exception ) { } return 0; }
function2()
main()
function1()
function2() main()
main()
int main( ) { try { try { throw 1; } catch( float ) { } } catch( int ) { } return 0; }
class DivideByZero { int numerator; public: DivideByZero(int i) { numerator = i; } void Print() const{ cout << endl << numerator << “ was divided by zero”; } };
int Quotient(int a, int b) {
if(b == 0){ throw DivideByZero(a); } return a / b;
}
for ( int i = 0; i < 10; i++ ) { try { GetNumbers(a, b); quot = Quotient(a, b); ... } catch(DivideByZero & obj) { obj.Print(); i--; } } cout << “\nSum of ten quotients is ” << sum;
Enter two integers 10 10 Quotient of 10 and 10 is 1 Enter two integers 10 0 10 was divided by zero ... // there will be sum of exactly ten quotients
MathError
DivideByZero
IntergerOutOfRange
try{ ... } catch(DivideByZero){ ... } catch(IntergerOutOfRange){ ... } catch (InputStreamError){ }
try{
...
}
catch (MathError){
}
catch (InputStreamError){
}
catch ( ... )
{
//...
}
throw; /without any expression/
int main ( ) { try { Function(); } catch(Exception&) { ... } return 0; }
void Function( ) { try { /Code that might throw an Exception/ } catch(Exception&){ if( can_handle_completely ) { // handle exception } else { // partially handle exception throw; //re-throw exception } } }