Why is it expensive to use objects for exception?
What is the main purpose of the constructor?
What is the output of this program?
1. #include2. using namespace std; 3. double division(int a, int b) 4. { 5. if (b == 0) 6. { 7. throw "Division by zero condition!"; 8. } 9. return (a / b); 10. } 11. int main () 12. { 13. int x = 50; 14. int y = 0; 15. double z = 0; 16. try 17. { 18. z = division(x, y); 19. cout << z << endl; 20. } 21. catch (const msg) 22. { 23. cerr << msg << endl; 24. } 25. return 0; 26. }
What is the output of this program?
1. #include2. #include 3. using namespace std; 4. int main () 5. { 6. int num = 3; 7. string str_bad = "wrong number used"; 8. try 9. { 10. if ( num == 1 ) 11. { 12. throw 5; 13. } 14. if ( num == 2 ) 15. { 16. throw 1.1f; 17. } 18. if ( num != 1 || num != 2 ) 19. { 20. throw str_bad; 21. } 22. } 23. catch (int a) 24. { 25. cout << "Exception is: " << a << endl; 26. } 27. catch (float b) 28. { 29. cout << "Exception is: " << b << endl; 30. } 31. catch (...) 32. { 33. cout << str_bad << endl; 34. } 35. return 0; 36. }
What is the output of this program?
1. #include2. #include 3. #include 4. using namespace std; 5. void func(int c) 6. { 7. if (c < numeric_limits :: max()) 8. throw invalid_argument("MyFunc argument too large."); 9. else 10. { 11. cout<<"Executed"; 12. } 13. } 14. int main() 15. { 16. try 17. { 18. func(256); 19. } 20. catch(invalid_argument& e) 21. { 22. cerr << e.what() << endl; 23. return -1; 24. } 25. return 0; 26. }