fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Ideone{
  5. public static int mult(int a, int b) throws ArithmeticException {
  6. long resultado = a * (long) b;
  7. if (resultado > Integer.MAX_VALUE || resultado < Integer.MIN_VALUE){
  8. throw new ArithmeticException("Integer overflow");
  9. }
  10. return (int) resultado;
  11. }
  12.  
  13. public static void main (String[] args) throws java.lang.Exception{
  14. int valor1 = 100;
  15. int valor2 = 22118400;
  16. int valor3 = 44954676;
  17.  
  18. int mul = mult(valor1, valor2);
  19. int divisao = mul / valor3;
  20. System.out.printf("A soma entre %d e %d resulta %d\n", valor1, valor2, mul);
  21. System.out.printf("A divisão entre %d e %d resulta %d", mul, valor3, divisao);
  22. }
  23. }
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArithmeticException: Integer overflow
	at Ideone.mult(Main.java:8)
	at Ideone.main(Main.java:18)