class="java" name="code">package com.exception; /** * 所有应用程序异常的父类 * 凡是检查性异常,都继承Exception * @author stevep * */ public abstract class ApplicationException extends Exception { private static final long serialVersionUID = 1L; public ApplicationException() { } public ApplicationException(String m) { super(m); } public ApplicationException(Throwable m) { super(m); } public ApplicationException(String m, Throwable t) { super(m, t); } }
?
package com.exception; /** * 所有非检查异常的父类 * 凡是运行时异常,都继承RuntimeException * @author stevep * */ public class ApplicationRuntimeException extends RuntimeException{ private static final long serialVersionUID = -3269956337283547411L; public ApplicationRuntimeException() { } public ApplicationRuntimeException(String m, Throwable t) { super(m, t); } public ApplicationRuntimeException(String m) { super(m); } public ApplicationRuntimeException(Throwable t) { super(t); } }
?
package com.exception; public class CheckingAccount { private double balance; private int number; public CheckingAccount(int number) { this.number = number; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) throws ApplicationRuntimeException { if (amount <= balance) { balance -= amount; } else { double needs = amount - balance; throw new ApplicationRuntimeException("needs:" + needs); } } public double getBalance() { return balance; } public int getNumber() { return number; } }
?
package com.exception; public class BankDemo { public static void main(String[] args) { CheckingAccount c = new CheckingAccount(101); System.out.println("Depositing $500..."); c.deposit(500.00); try { System.out.println("\nWithdrawing $100..."); c.withdraw(100.00); System.out.println("\nWithdrawing $600..."); c.withdraw(600.00); } catch (ApplicationRuntimeException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
?