Exception

Reading assignment

You should read : Lesson: Handling Errors with Exceptions(The whole lesson!)

Create your own exceptions

You can get the all programs here.
Save the work in bank folder.
NegativeInputException.java
   1:package bank;
   2:
   3:public class NegativeInputException extends Exception {
   4:    public NegativeInputException() {
   5:        super();
   6:    }
   7:    
   8:    public NegativeInputException(String s) {
   9:        super(s);
  10:    }
  11:}
NotEnoughMoneyException.java
   1:package bank;
   2:
   3:public class NotEnoughMoneyException extends Exception {
   4:    public NotEnoughMoneyException() {
   5:        super();
   6:    }
   7:    
   8:    public NotEnoughMoneyException(String s) {
   9:        super(s);
  10:    }
  11:}
TooLargeException.java
   1:package bank;
   2:
   3:public class TooLargeException extends RuntimeException {
   4:    public TooLargeException() {
   5:        super();
   6:    }
   7:    
   8:    public TooLargeException(String s) {
   9:        super(s);
  10:    }
  11:}
Balance.java
   1:package bank;
   2:
   3:public class Balance {
   4:    // cannot deposit money more than 1 million dollars
   5:    public static int tooLarge = 1000000; 
   6:    
   7:    private int balance;
   8:    
   9:    public Balance(int balance) {
  10:        this.balance = balance;
  11:    }
  12:    
  13:    public int getBalance() {
  14:        return balance;
  15:    }
  16:    
  17:    public void withdraw(int amt) throws NegativeInputException, 
  18:                                         NotEnoughMoneyException
  19:    {
  20:        if(amt < 0) {
  21:            throw new NegativeInputException("Withdraw amount " +
  22:                                            " cannot be negative");
  23:        } else if (amt > balance) {
  24:            throw new NotEnoughMoneyException();
  25:        } else {
  26:            balance-=amt;
  27:        }
  28:    }
  29:    
  30:    public void deposit(int amt) throws NegativeInputException {
  31:        if(amt < 0) {
  32:            throw new NegativeInputException("Deposit amount " + 
  33:                                             "cannot be negative");
  34:        } else if(amt > tooLarge) {
  35:            throw new TooLargeException("Cannot deposit more than " +
  36:                                         tooLarge + " dollars");
  37:        } else {
  38:            balance+=amt;
  39:        }
  40:    }
  41:}
Test.java
You can get the program here.
   1:import bank.*;
   2:
   3:public class Test {
   4:    public static void main(String[] args) {
   5:        Balance b = new Balance(100);
   6:        try {
   7:            b.withdraw(200);
   8:        } catch (NegativeInputException ex) {
   9:            ex.printStackTrace();
  10:        } catch (NotEnoughMoneyException ex) {
  11:            ex.printStackTrace();
  12:        }
  13:        
  14:        try {
  15:            b.deposit(-100);
  16:        } catch (NegativeInputException ex) {
  17:            ex.printStackTrace();
  18:        }
  19:        
  20:        try {
  21:            b.deposit(10000000);
  22:        } catch (NegativeInputException ex) {
  23:            ex.printStackTrace();
  24:        }
  25:        
  26:    }
  27:}
Output
bank.NotEnoughMoneyException
   at bank.Balance.withdraw(Balance.java:24)
   at Test.main(Test.java:7)
bank.NegativeInputException: Deposit amount cannot be negative
   at bank.Balance.depposit(Balance.java:32)
   at Test.main(Test.java:15)
Exception in thread "main" bak.TooLargeException: Cannot deposit
more than 1000000 dollars
   at bank.Balance.deposit(Balance.java:35)
   at Test.main(Test.java:21)

finally block

FinalTest.java
You can get the program here.
   1:public class FinalTest {
   2:    public static void main(String[] args) {
   3:        String s = "11";
   4:        int x;
   5:        System.out.println("Test 1: no exception is thrown");
   6:        test(s);
   7:        
   8:        s = "abc";
   9:        System.out.println("Test 2: exception is thrown");
  10:        test(s);
  11:    }
  12:    
  13:    public static void test(String s) {
  14:        try {
  15:            Integer.parseInt(s);
  16:        } catch (NumberFormatException ex) {
  17:            System.out.println("NumberFormatException");
  18:        } finally {
  19:            System.out.println("Reaches finally block");
  20:        }
  21:    }
  22:}
Output
Test 1: no exception is thrown
Reaches finally block
Test 2: exception is thrown
NumberFormatException
Reaches finally block
Question
If you use System.exit in the try block, can the finally block be reached?

FinalTest2.java
You can get the program here.
   1:public class FinalTest2  {
   2:    public static void main(String[] args) {
   3:        System.out.println("test for 11");
   4:        System.out.println(test("11"));
   5:        System.out.println("test for abc");
   6:        System.out.println(test("abc"));
   7:    }
   8:    
   9:    public static String test(String s) {
  10:        int x;
  11:        try {
  12:            x = Integer.parseInt(s);
  13:            return "OK";
  14:        } catch (NumberFormatException ex) {
  15:            return "Exception";
  16:        } finally {
  17:            return "finally";
  18:        }
  19:    }
  20:}     
Output
test for 11
finally
test for abc
finally