Advertisement
karlakmkj

Exception with throw

Sep 6th, 2020 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.41 KB | None | 0 0
  1. package javaPack;
  2.  
  3. public class ThrowException {
  4.    
  5.     public static double divide(double x, double y) {
  6.         if (y==0) {
  7.             throw new ArithmeticException("Divider cannot be zero!");
  8.         }
  9.         return x/y;
  10.     }
  11.    
  12.     public static void main(String[] args) {
  13.         double d;
  14.         try {
  15.             d = divide(8.4,0);
  16.             System.out.println("Result: " +d);
  17.         }
  18.         catch(ArithmeticException e) {
  19.             System.out.println(e);
  20.         }
  21.     }
  22. }  
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement