Advertisement
Guest User

divby0.cxx

a guest
Mar 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. #include <exception>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class divby0 : public exception
  7. {
  8.     const char *what() const throw() { return "Cannot divide by 0"; }
  9. };
  10.  
  11. int divide(int a, int b)
  12. {
  13.     if (b == 0)
  14.         throw divby0();
  15.     return a / b;
  16. }
  17.  
  18. int main()
  19. {
  20.     try
  21.     {
  22.         int a = 10;
  23.         int b = 5;
  24.         int c = 0;
  25.         int d = 1;
  26.         cout << a << " / " << b << " = " << divide(a, b) << endl;
  27.         cout << a << " / " << c << " = " << divide(a, c) << endl;
  28.         cout << a << " / " << d << " = " << divide(a, d) << endl;
  29.     }
  30.     catch (std::exception &e)
  31.     {
  32.         cout << e.what() << endl;
  33.     }
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement