Java基础11--Object类--异常机制(三)

2014-11-24 07:42:56 · 作者: · 浏览: 2
骤:

(1)调用method方法时,method方法声明了抛出的异常,传入的-30满足index<0的条件,用throw抛出了异常,抛给main中的调用者,调用者用trycatch对异常进行了解决。

(2)满足index<0抛出异常。

(3)由于用trycatch形式处理异常,会直接在catch中解决,输出”负数角标异常”,之后的”over”也输出。

11-12,异常-多catch情况

throws声明异常可以有多个,用逗号隔开,用下例说明:

class FuShuIndexException extends Exception {
	FuShuIndexException() {}
	FuShuIndexException(String msg) {
		super(msg);
	}
}
class Demo {
	public int method(int[] arr, int index) throws NullPointerException,FuShuIndexException {
		if(arr == null) 
			throw new NullPointerException("没有任何数组实体");
		if(index < 0)
			throw new FuShuIndexException();
		return arr[index];
	}
}
class ExceptionDemo {
	public static void main(String[] args) {
		int[] arr = new int[3];
		Demo d = new Demo();
		try {
			//在try后面,一般都先执行第一个catch,若Exception在最上面,则其他catch白写。
			int num = d.method(null, -1);
			System.out.println("num = " + num);
		} catch(NullPointerException e) {
			System.out.println(e.toString());
		} catch(FuShuIndexException e) {
			System.out.println("message:" + e.getMessage());
			System.out.println("String:" + e.toString());
			e.printStackTrace();
			System.out.println("负数角标异常");
		} catch(Exception e) {}
		/*
		多catch情况,父类的catch放在最下面
		Exception是所有异常类的父类,若可能有抛出的以外的其他异常的情况发生,
		需要使用Exception解决时,Exception必须放在最下面。
		*/
		System.out.println("over");
	}
}

11-13,异常处理原则

1,原则:

(1)函数内部如果抛出需要检测的异常,那么函数上必须声明。否则必须在函数内使用trycatch捕捉,否则编译失败。

(2)如果调用了声明异常的函数,要么trycatch要么throws,否则编译失败。

(3)什么时候用catch,什么时候用throws呢?

功能内部可以解决用catch;

解决不了,用throws告诉调用者,由调用者解决。

(4)一个功能如果抛出了多个异常,那么调用时,必须有对应的多个catch进行针对性的处理。内部有几个需要检测的异常,就抛出几个异常,抛出几个就catch几个。

11-14,异常-finally代码块

1,finally通常用于关闭(释放)资源,以下例说明,finally不管怎样都会执行,但若在finally前使用了System.exit(0);则finally不执行,System.exit(0);直接退出了JVM。

class Demo {
	public int show(int index) throws ArrayIndexOutOfBoundsException {
		if(index < 0)
			throw new ArrayIndexOutOfBoundsException("角标越界");
		int[] arr = new int[3];
		return arr[index];
	}
}
class ExceptionDemo {
	public static void main(String[] args) {
		Demo d = new Demo();
		try {
			int num = d.show(-1);
			System.out.println("num:" + num);
		} catch(ArrayIndexOutOfBoundsException e) {
			System.out.println(e.toString());
			//return;退出main函数,只输出e.toString()和finally中的内容
			//System.exit(0);退出JVM,只输出e.toString()
		} finally {//通常用于关闭(释放)资源
			System.out.println("finally");
		}
		System.out.println("over");
	}
}

2,例如:

连接数据库

查询:抛出异常Exception

关闭连接

在连接数据库后,进行查询的时候抛出了异常,若没有finally去结束链接,则数据库不会断开连接,会占用大量资源。Finally是无论怎样都会执行的代码块,若在抛出异常后,在finally中结束链接,则会释放资源。

3,try,catch,finally代码的组合特点

(1)try-catch-finally常规组合

(2)try catch(多个)

当没有必要资源需要释放的时候,可以不定一finally。

(3)try-finally

异常无法直接用catch处理,但是资源需要关闭时使用。

如:

void show() throws Exception { //方法中有抛出异常,需要声明
	try{
		//开启资源
		//抛出异常后无法正常关闭资源,用finally关闭,这个异常无法用catch直接处理
		throw new Exception();
	} finally {
		//关闭资源
	}
}

11-15,异常应用

需求:老师用电脑上课

这个问题涉及两个对象,老师和电脑。

其中可能产生的问题:比如电脑蓝屏,冒烟,没响应。

//首先定义三个异常类
class LanPingException extends Exception {
	LanPingException() {}
	LanPingException(String msg) {
		super(msg);
	}
}
class MaoYanException extends Exception {
	MaoYanException() {}
	MaoYanException(String msg) {
		super(msg);
	}
}
class NoPlanException extends Exception {
	NoPlanException() {}
	NoPlanException(String msg) {
		super(msg);
	}
}
class Computer {
	//初始化状态,0表示正常,1表示蓝屏,2表示冒烟
	private state = 0;
	public void run() throws LanPingException,MaoYanException {
		if(state == 1)
			throw new LanPingException("电脑蓝屏了!");
		if(state == 2)
			throw new MaoYanException("电脑冒烟了!");
		System.out.println("电脑运行");
	}
	public void reset() {
		state = 0;
		System.out.println("电脑重启");
	}
}
class Teacher {
	private String name;
	private Computer comp;
	Teacher(String name) {
		this.name = name;
		//老师携带电脑,在初始化时创建Computer对象
		comp = new Computer();
	}
	//捕获的冒烟异常中抛出了此异常
	public void prelect() throws NoPlanException {
		try {
			comp.run();
			System.out.println(name + "讲课");
		} catch(LanPingException e) {
			System.out.println(e.toString());
			comp.reset();
			prelect();
		} catch(MaoYanException e) {
			System.out.println(e.toString());
			test();
			throw new NoPlanException("课时进度无法完成,原因:" + e.getMessage());
			/*
			这里虽然抛出的是冒烟异常,但是抛出后上级无法解决,所以抛出计划无法完成异常,
			让上级能够解决。抛出异常和声明异常应根据实际情况而定。
			*/
		}
	}
	public void test() {
		System.out.println("大家练习");
	}
}
class ExceptionTest {
	public static void main(String[] args) {
		Teacher t = new Teacher("老师");
		try {
			t.prelect();
		} catch(NoPlanException e) {
			System.out.println(e.toString() + "...");
			System.out.println("换老师");
		}
	}
}