1
0

17_divby0.cpp 576 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <exception>
  2. #include <iostream>
  3. using namespace std;
  4. class divby0 : public exception
  5. {
  6. const char *what() const throw() { return "Cannot divide by 0"; }
  7. };
  8. int divide(int a, int b)
  9. {
  10. if (b == 0)
  11. throw divby0();
  12. return a / b;
  13. }
  14. int main()
  15. {
  16. try
  17. {
  18. int a = 10;
  19. int b = 5;
  20. int c = 0;
  21. int d = 1;
  22. cout << a << " / " << b << " = " << divide(a, b) << endl;
  23. cout << a << " / " << c << " = " << divide(a, c) << endl;
  24. cout << a << " / " << d << " = " << divide(a, d) << endl;
  25. }
  26. catch (std::exception &e)
  27. {
  28. cout << e.what() << endl;
  29. }
  30. return 0;
  31. }