fork(1) download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <limits>
  4.  
  5. int multiply(int a, int b) {
  6. if (a > 0 && b > 0 && a > (std::numeric_limits<int>::max() / b)) {
  7. throw std::overflow_error("Overflow detected");
  8. }
  9. return a * b;
  10. }
  11.  
  12. int main() {
  13. try {
  14. int x = 50000;
  15. int y = 50000;
  16. int result = multiply(x, y);
  17. std::cout << "Result: " << result << std::endl;
  18. } catch (const std::overflow_error& e) {
  19. std::cout << "Error: " << e.what() << std::endl;
  20. }
  21.  
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Error: Overflow detected