java的异常处理机制
异常处理机制是java中为应对程序运行中发生的异常,预先想好一定的办法,当异常发生时,使程序按照预定的处理办法对异常进行处理。java的异常处理是通过try、catch、finally、throw和throws五个关键字来实现的。
try-catch关键字
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
System.out.print("请输入被除数:");
int a = input.nextInt();
System.out.print("请输入除数:");
int b = input.nextInt();
System.out.println("a/b=" + (a / b));
} catch (Exception e) {
System.err.println("出现错误,被除数和除数必须是整数,除数不能为0");
e.printStackTrace();
}
}
try-cathc语句首先执行的是try语句块的语句。如果try语句块中的语句正常执行完毕,没有发生异常,那么就会直接输出a/b的结果,catch语句块将不再执行。
如果try语句块中执行发生异常,如除数为0那么将不再输出a/b,而是调到 catch语句,打印发生的错误。
try-catch-finally处理异常
try-catch-finally语句相当于是对try-catch的一种补充。
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
System.out.print("请输入被除数:");
int a = input.nextInt();
System.out.print("请输入除数:");
int b = input.nextInt();
System.out.println("a/b=" + (a / b));
} catch (Exception e) {
System.err.println("出现错误,被除数和除数必须是整数,除数不能为0");
e.printStackTrace();
}finally {
System.out.println("感谢使用");
}
}
只需要在catch后面加入finally语句块即可。无论是否发生异常,finally语句块都会执行。
try语句块中执行正确时:
try语句块发生异常时:
try-catch-finally结构中,try是必须要存在的,catch和finally语句块为可选,但两者至少出现一个。
throws声明抛出异常
如果在一个方法体中抛出了异常,并希望调用者能够及时的捕获异常,我们可以通过throws声明某个方法可能抛出的各种异常用来通知调用者。throws可以同时声明多个异常,之间用逗号隔开。
public class Test2 {
public static void divide() throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("请输入被除数:");
int a = input.nextInt();
System.out.print("请输入除数:");
int b = input.nextInt();
System.out.println("a/b=" + (a / b));
}
public static void main(String[] args) {
try {
divide();
} catch (InputMismatchException e) {
System.err.println("被除数和除数必须是整数");
} catch (ArithmeticException e) {
System.err.println("除数不能为0");
} catch (Exception e) {
System.err.println("其他未知错误");
}finally {
System.out.println("感谢使用!");
}
}
}
通过try-catch捕获异常,通过throws继续声明异常。如果调用者不知道如何处理该异常,可以继续用过thrwos声明异常,让上一级调用者处理异常。
使用throw抛出异常
public class Person {
private String name = "阿伟";
private int age;
public void setAge(int age)throws Exception {
if (age >= 1 && age <= 100) {
this.age=age;
System.out.println(this.name + "的年龄是:" + age);
} else {
throw new Exception("阿伟已经死了!");
}
}
public static void main(String[] args) {
Person p=new Person();
try {
p.setAge(500);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}finally {
System.out.println("你挑的嘛偶像!");
}
}
}
除了系统自动抛出异常外,在编程过程中,有些问题系统无法自动判定识别,如年龄不再正常范围内。此时需要程序员而不是系统来习性抛出异常。把问题提交给调用者来处理。在上面的例子中。我们设置的年龄为500,显然超出了正常的范围,但是如果我们不选择自行抛出,系统本身是无法辨别是否对错的。因为我们不论输入500还是50,系统只会认为这是一个整形,是正确的数字,所以就需要我们自己去抛出这一异常。
throw和throws的区别
- 作用不同:throw用于程序自行产生并抛出异常,throws用于声明该方法内抛出了异常。
- 使用的位置不同:throw位于方法体内部,可以作为单独语句使用;throws必须跟在方法参数列表的后面,不能单独使用;
- 内容不同:throw抛出一个异常对象,只能是一个;throws后面跟异常类,可以跟多个
常见的异常类
-
Exception:异常层次结构的根类
-
ArithmeticExceptio:算数错误异常,如以0为除数;
-
ArrayIndexOutOfBoundsException:数组下标越界
-
NullPointerException:尝试访问null对象成员;
-
ClassNotFoundException:不能加载所需的类;
-
InputMismatchException:欲得到的数据类型和输入的不符;
-
IllegalArgumentExc:方法得到非法参数
-
ClassCastException:对象强制类型转换出错
-
NumberFormatExc:数字格式转换异常,如吧“abc”转换成数字