ariestamirra

ex1

May 22nd, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package Percobaan;
  2.  
  3. class counterException extends Exception { // Define
  4.  
  5.     String complaint;
  6.  
  7.     public counterException(String c) {
  8.         this.complaint = c;
  9.     }
  10.  
  11.     public String toString() {
  12.         return "counter Exception " + complaint;
  13.     }
  14. }
  15.  
  16. class counter {
  17.  
  18.     int n = 0;
  19.  
  20.     public int zero() {
  21.         return n = 0;
  22.     }
  23.  
  24.     public int up() {
  25.         return ++n;
  26.     }
  27.  
  28.     public int down() throws counterException { // Throw
  29.         if (n <= 0) {
  30.             throw new counterException(n + " count Down failed.");
  31.         }
  32.         return --n;
  33.  
  34.     }
  35. }
  36.  
  37. public class Example1 {
  38.  
  39.     public static void main(String args[]) {
  40.         counter aCounter = new counter();
  41.         aCounter.zero();
  42.         aCounter.up();
  43.         try {
  44.             aCounter.down();
  45.         } catch (counterException ce) { // Catch
  46.             System.out.println("" + ce);
  47.         }
  48.         try {
  49.             aCounter.down();
  50.         } catch (counterException ce) { // Catch
  51.             System.out.println("" + ce);
  52.         } finally {
  53.             System.out.println("Finally");
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment