20-throw.java 475 B

1234567891011121314151617181920
  1. class Main {
  2. public static void main(String[] args) {
  3. try {
  4. double result = divide(10, 0);
  5. System.out.println("Resultado = " + result);
  6. } catch (ArithmeticException e) {
  7. System.out.println(e.getMessage());
  8. }
  9. }
  10. public static double divide(int numerator, int denominator) {
  11. if (denominator == 0) {
  12. throw new ArithmeticException("No se puede realizar una división entre cero!!");
  13. }
  14. return numerator / denominator;
  15. }
  16. }