Advertisement
lamaulfarid

ExeptionHandlingThrow

Dec 23rd, 2020
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.  
  2. /**
  3.  * Class untuk penanganan error dengan throw keyword
  4.  *
  5.  * @author Ahmad Lamaul Farid
  6.  * @version 24 Desember 2020
  7.  */
  8.  
  9. public class Main
  10. {
  11.     static void checkAge(int age)
  12.     {
  13.         if (age < 18)
  14.         {
  15.             throw new ArithmeticException("Access denied - You must be at least 18 years old.");
  16.         }
  17.         else
  18.         {
  19.             System.out.println("Access granted - You are old enough!");
  20.         }
  21.     }
  22.    
  23.     public static void main(String[] args)
  24.     {
  25.         checkAge(15); // Set age to 15 (which is below 18...)
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement