Java异常总结(二)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java异常总结(二)

Java异常总结(二)

 2015/1/8 18:22:01  tuoni  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.exception;/***所有应用程序异常的父类*凡是检查性异常,都继承Exception*@authorstevep**/publicabstractclassApplicationExceptionextendsException{privatestaticfinallongserialVersionUID=1L;publicApplicationException(){}publicApplicationException(Stringm){super(m);
  • 标签:总结 Java 异常
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();
		}
	}
}

?

发表评论
用户名: 匿名