Java异常总结(二)

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();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值