Dwinanda

Error Handling(Try Catch)

Dec 28th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1.  
  2. /**
  3.  * class untuk handling exception  menggunakan try dan catch
  4.  *
  5.  * @author Dwinanda Bagoes Ansori
  6.  * @version 29 Desember 2020
  7.  */
  8. import java.util.Scanner;
  9. import java.util.InputMismatchException;
  10.  
  11. public class TryCatch
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         Scanner input = new Scanner(System.in);
  16.         boolean continu = true;
  17.        
  18.         do
  19.         {
  20.             try
  21.             {
  22.                 System.out.print("Input Integer: ");
  23.                 int number = input.nextInt();
  24.                
  25.                 //Menampilkan hasil
  26.                 System.out.println();
  27.                 System.out.println("Ur Number :  " + number);
  28.                 continu = false;
  29.             }
  30.            
  31.             catch(InputMismatchException ex)
  32.             {
  33.                 System.out.println();
  34.                 System.out.println("Invalid Input!!! Input integer.");
  35.                 input.nextLine();
  36.             }
  37.         } while(continu);
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment