Java 编程思想 7 (异常处理机制--支持层层上报)

摘要: 异常 (Exception) 作为名词, 是指出错报告, 或故障报告. 异常模拟了人类的故障处理机制.

1. 栗子

顾客张三到餐馆: 老板, 来份番茄炒蛋.
老板李四到后厨: 王五, 做份番茄炒蛋.
厨师王五正准备开做, 发现没番茄了.
怎么办?

方案 A

王五直接冲出厨房对张三大声嚷嚷: 唉呀呀不得了, 出现 8527 号内部错误 !
然后餐馆关门大吉.

方案 B

王五: 老板, 没番茄了耶, 你怎么办?
李四: 我到隔壁菜市场买番茄.
李四: 番茄买到了, 你做吧.

分析

方案 B 就是异常处理机制.
王五所说的 “没番茄” 了, 就是异常报告.

2. 代码

package thinking;

public class ExceptionTest {
	public static void main(String[] args) {
		Chef wangwu = new Chef();
		Boss lisi = new Boss(wangwu);
		Customer zhangsan = new Customer(lisi);

		for (int i = 0; i < 3; i++) {
			try {
				zhangsan.order();
			} catch (Exception ee) {
				System.out.println(ee);
			} // Of try
		} // Of for i
	}// Of main
}// Of class ExceptionTest

class Customer {
	Boss myProvider;

	Customer(Boss paraBoss) {
		myProvider = paraBoss;
	}// Of the constructor

	void order() throws Exception {
		System.out.println("---------");
		System.out.println("Customer orders a dish.");
		myProvider.serve();
		System.out.println("Customer gets the dish.");
	}// of order
}// Of class Customer

class Boss {
	Chef myChef;

	Boss(Chef paraChef) {
		myChef = paraChef;
	}// Of the constructor

	void serve() throws Exception {
		try {
			System.out.println("Boss assigns the chef to cook.");
			myChef.cook();
			return;
		} catch (MaterialException me) {
			buy(me.type);
		} // Of try

		try {
			System.out.println("Boss assigns the chef to cook (again).");
			myChef.cook();
		} catch (MaterialException me) {
			System.out.println("The boss has no idea.");
			throw new Exception("This is a real error.");
		} // Of try
	}// Of serve

	void buy(int paraType) {
		if (paraType == 0) {
			System.out.println("Boss buys eggs for chef.");
			myChef.eggCount += 5;
		} else if (paraType == 1) {
			System.out.println("Boss buys tomatos for chef.");
			myChef.tomatoCount += 5;
		} // Of if
	}// Of buy
}// Of class Boss

class Chef {
	int eggCount;
	int tomatoCount;

	Chef() {
		eggCount = 1;
		tomatoCount = 2;
	}// Of the constructor

	void cook() throws MaterialException {
		System.out.println("Chef tries to cook.");
		if (eggCount == 0) {
			System.out.println("Chef finds no egg.");
			throw new MaterialException(0);
		} else if (tomatoCount == 0) {
			System.out.println("Chef finds no tomato.");
			throw new MaterialException(1);
		} // Of if

		eggCount--;
		tomatoCount--;
	}// Of cook
}// Of class Chef

class MaterialException extends Exception {
	int type;

	MaterialException(int paraType) {
		type = paraType;
	}// Of the constructor
}// Of class MaterialException

运行结果如下

---------
Customer orders a dish.
Boss assigns the chef to cook.
Chef tries to cook.
Customer gets the dish.
---------
Customer orders a dish.
Boss assigns the chef to cook.
Chef tries to cook.
Chef finds no egg.
Boss buys eggs for chef.
Boss assigns the chef to cook (again).
Chef tries to cook.
Customer gets the dish.
---------
Customer orders a dish.
Boss assigns the chef to cook.
Chef tries to cook.
Chef finds no tomato.
Boss buys tomatos for chef.
Boss assigns the chef to cook (again).
Chef tries to cook.
Customer gets the dish.

3. 代码分析

  1. 顾客要向老板传递消息, 因此 Customer 里面有 Boss 的引用.
  2. 老板要向主厨传递消息, 因此 Boss 里面有 Chef 的引用.
  3. 本例中只有一个菜: 番茄炒蛋.
  4. 老板根据主厨的报告 (缺什么材料) 来决定买啥.
  5. 由于初始化时厨师有一个鸡蛋两个番茄, 第一轮顺利做出来. 第二轮缺鸡蛋老板去买; 第三轮缺番茄老板去买.
  6. 主厨给的出错报告 (MaterialException) 里面只有缺的材料类型, 而老板的出错报告 (Exception) 里面写的是他对程序猿的控诉: 你想周全点行不?
  7. 这是我能想到的关于异常最小的例子了. 如果你的代码不到 500 行, 就不值得用 Exception.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值