Advertisement
karlakmkj

Exception - Throwable methods

Sep 6th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. package javaPack;
  2.  
  3. public class ThrowableMethods {
  4.  
  5.     public static double divide(double x, double y) {
  6.         if (y==0) {
  7.             throw new ArithmeticException("Arithmetic Exception occurred");
  8.         }
  9.         return x/y;
  10.     }
  11.    
  12.     public static void main(String[] args) {
  13.         double d;
  14.         try {
  15.             d = divide(5.6,0);
  16.         }
  17.         catch(ArithmeticException e) {
  18.             //System.out.println("Divider cannot be equal to zero");
  19.            
  20.             //useful methods
  21.             System.out.println(e.getMessage()); //print above from divide() method
  22.             System.out.println(e.toString());
  23.             e.printStackTrace();
  24.         }
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement