Java异常机制

1. 什么是Java异常机制

Java异常是Java提供的用于处理程序中错误的一种机制。所谓错误是指在程序运行过程中发生的一些异常事件(如:0溢出,数组下标越界,所要读取的文件不存在等)。Java程序的执行过程中如果出现异常事件,可以生成一个异常类对象,该异常类对象封装了异常事件的信息并提交给Java运行时系统,这个过程称为抛出(throws)异常。当Java运行时系统收到异常对象时,会寻找能处理这一对象的代码,并把当前异常对象交给其处理,这一个过程称为捕获(catch)异常。

        如果没有对异常进行处理,则程序运行过程中会报错,程序也无法继续执行。举个简单的例子:

public class TestException {
	public static void main(String args[]) {
		System.out.println(2/0);
		System.out.println(2/1);
	}
}

运行上面的代码,结果如下:


如果我们捕获了异常并且对异常进行了恰当的处理,不但可以帮助我们开发人员定位问题,也不会打断程序继续执行,同时也可以给用户更好的提示。增加异常处理后的代码如下:
public class TestException {
	public static void main(String args[]) {
		try{
			System.out.println(2/0);
		}catch (ArithmeticException ae) {
			ae.printStackTrace();
			System.out.println("除数不能为0");
		}
		
		System.out.println(2/1);
	}
}






















运行上面的代码,结果如下:


2. 五个关键字

1.try—— try关键字后的一段代码表示这段代码可能会抛出异常。

2.catch——catch关键字用来捕获异常并对异常进行处理。catch后的一段代码用来进行异常的处理。每个try语句块可以伴随一个或多个catch代码块,用来处理可能产生的多个不同类型的异常对象。

3.throw——throw关键字用于语句抛出一个异常。举个例子:

public class TestException2 {
	public static void main(String args[]) {
		try{
			new TestException().testEx(0);
		} catch (ArithmeticException ae) {
			ae.printStackTrace();
		}
		new TestException().testEx(1);
	}
	
	private void testEx(int i) {
		if (i == 0) {
			throw new ArithmeticException("除数不能为0");
		} else {
			System.out.println(2/i);
		}
	}
} 

4. throws——throws关键字用于声明一个方法可能会抛出异常。举个栗子:

public class TestException3 {
	public static void main(String args[]) {
		try{
			new TestException().testEx(0);
		} catch (ArithmeticException ae) {
			ae.printStackTrace();
		}
		new TestException().testEx(1);
	}
	
	private void testEx(int i) throws ArithmeticException {
		System.out.println(2/i);
	}
}

5. finally——finally关键字后的代码块为异常处理提供一个统一的出口,使得控制流程在转入其他部分之前能够对程序的状态做统一的管理。不论try代码块是否抛出异常,fianlly所指定代码块都会执行。通常在finally代码块中做一些资源的清除工作。比如关闭打开的文件,删除临时文件等。举个例子:

public class TestException {	
	public static void main(String args[]) {
		FileInputStream in = null;
		try {
			in = new FileInputStream("mytext.txt");
			int b;
			try {
				b = in.read();
				while(b != -1) {
					System.out.print((char) b);
					b = in.read();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
} 


3. Java异常类的划分

Java中所有异常类的父类是Throwable。Throwable包含两个子类:Error和Exception,其中Error是系统异常,是我们无法干预的,我们只需要关心Exception类。Exception类包含RuntimeException类和其他子类,RuntimeException是指运行时的一些异常,类似于数组越界等这种异常,这种异常太多了通常我们都不需要去处理,如果每个异常都要try catch,那写代码就太麻烦了,我们只需要关注除了RuntimeException类外的Exception类的子类就可以了。Java异常类的划分如下图:


4. 需要注意的几点

1. 如果try代码块后跟随多个catch语句,那么catch语句的顺序为子类异常的catch语句在前,父类异常的catch语句在后。

2. 如果重写一个可能会抛出异常的方法时,重写方法可以不抛异常,如果抛异常,必须和原方法所抛出的异常类型一致。

3. 注意throw和throws关键字的区别,切勿用错。


        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值