Advertisement
kuathadianto

Exception Example

Nov 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. // ACHTUNG! Ada 2 file disini!
  2. // CekNomor.java
  3.  
  4. public class CekNomor {
  5.  
  6.     public CekNomor(String input) throws BukanNomorException {
  7.         for(int i = 0; i < input.length(); i++) {
  8.             if(input.charAt(i) < '0' || input.charAt(i) > '9') {
  9.                 throw new BukanNomorException("Index " + i + " bukan nomor!");
  10.             }
  11.         }
  12.     }
  13.    
  14.     public static void main(String[] args) throws BukanNomorException {
  15.         new CekNomor("12345"); // harusnya nggak ada Meldung
  16.         new CekNomor("12a45"); // ada Meldung
  17.     }
  18. }
  19.  
  20. // BukanNomorException.java
  21.  
  22. public class BukanNomorException extends Exception {
  23.  
  24.     public BukanNomorException(String message) {
  25.         super(message);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement