Dwinanda

Error Handling(Throw)

Dec 28th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1.  
  2. /**
  3.  * class untuk handling exception  menggunakan try, catch, dan throw
  4.  *
  5.  * @author Dwinanda Bagoes Ansori
  6.  * @version 29 Desember 2020
  7.  */
  8. import java.util.Scanner;
  9.  
  10. public class ThrowTest
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         Scanner input = new Scanner(System.in);
  15.         int komodo;
  16.        
  17.         System.out.println("Bro tolong angkat komodo ke truk dong, truknya cukup 4 komodo bro");
  18.         System.out.println();
  19.        
  20.         try
  21.         {
  22.             System.out.print("Jumlah komodo yang diangkat : ");
  23.             komodo = input.nextInt();
  24.            
  25.             if (komodo > 4)
  26.             {
  27.                 throw new Exception("waduh kelebihan bro, gacukup truknya");
  28.             }
  29.            
  30.             else
  31.             {
  32.                 System.out.println("Yoi mantap cukup bro");
  33.             }
  34.         }
  35.        
  36.         catch(Exception e)
  37.         {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment