以异常类的形式对这些非正常情况进行封装,通过异常处理机制对程序运行时发生的各种问题进行处理
package test;
public class test1 {
public static void main(String[] args) {
System.out.println(a(1, 2));
System.out.println("aaaaaaaaaaaaa");
}
//当我们定义的被除数为整型时(short、int、long)会抛出此异常, 被除数为整型时不可为零。
public static int a(int x, int y) {
int result = x / y;
return result;
}
}
通过一张图来展示Throwable类的继承体系,如下图所示
通过图中可以看出,Throwable有两个直接子类Error和Exception,其中Error代表程序中产生的错误,Exception代表程序中产生的异常
Error类称为错误类,它表示Java运行时产生的系统内部错误或资源耗尽的错误,是比较严重的,仅靠修改程序本身是不能恢复执行的。
举一个生活中的例子,在盖楼的过程中因偷工减料,导致大楼坍塌,这就相当于一个Error。使用java命令去运行一个不存在的类就会出现Error错误。
Exception类称为异常类,它表示程序本身可以处理的错误,在开发Java程序中进行的异常处理,都是针对Exception类及其子类。在Exception类的众多子类中有一个特殊的RuntimeException类,该类及其子类用于表示运行时异常,除了此类,Exception类下所有其他的子类都用于表示编译时异常。
Throwable类中的常用方法:
异常捕获通常使用try…catch语句,具体语法格式如下:
其中在try代码块中编写可能发生异常的Java语句,catch代码块中编写针对异常进行处理的代码。当try代码块中的程序发生了异常,系统会将这个异常的信息封装成一个异常对象,并将这个对象传递给catch代码块。catch代码块需要一个参数指明它所能够接收的异常类型,这个参数的类型必须是Exception类或其子类。
public class test1 {
public static void main(String[] args) {
try {
int c = divide(4, 0);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("this is finally");
}
System.out.println("happy ending");
}
public static int divide(int x, int y) {
int result = x / y;
return result;
}
}
如果去调用一个别人写的方法时,是否能知道别人写的方法是否会有异常呢?这是很难做出判断的。针对这种情况,Java中允许在方法的后面使用throws关键字对外声明该方法有可能发生的异常,这样调用者在调用方法时,就明确地知道该方法有异常,并且必须在程序中对异常进行处理,否则编译无法通过。
throws关键字声明抛出异常的语法格式如下:
从上述语法格式中可以看出,throws关键字需要写在方法声明的后面,throws后面需要声明方法中发生异常的类型,通常将这种做法称为方法声明抛出一个异常
public class test1 {
public static void main(String[] args) {
System.out.println(divide(4, 0));
System.out.println("happy ending");
}
public static int divide(int x, int y) throws RuntimeException {
int result = x / y;
return result;
}
}
try…catch和finally
package test;
public class test2{
public static void main(String[] args) {
try{
int c=a(4,0);
}catch (ArithmeticException e){
System.out.println(e.getMessage());
}finally {
System.out.println("this is");
}
System.out.println("ending");
}
public static int a(int x,int y){
int result=x/y;
return result;
}
}
throws关键字
package test;
public class test3 {
public static void main(String[] args) {
//case1
System.out.println(a(4, 10));
//case2
try {
System.out.println(a(4, 0));
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("end");
}
//throws关键字需要写在方法声明的后面,throws后面需
//要声明方法中发生异常的类型,通常将这种做法称为方法声明抛出一个异常。
public static int a(int x,int y) throws RuntimeException{
int result=x/y;
return result;
}
}
编译时异常和运行时异常
在Java中,Exception类中除了RuntimeException类及其子类都是编译时异常。
编译时异常的特点是Java编译器会对其进行检查,如果出现异常就必须对异常进行处理,否则程序无法通过编译。处理编译时期的异常有两种方式:
• 使用try…catch语句对异常进行捕获
• 使用throws关键字声明抛出异常,调用者对其处理。
RuntimeException类及其子类都是运行时异常。运行时异常的特点是Java编译
器不会对其进行检查,也就是说,当程序中出现这类异常时,即使没有使用
try…catch语句捕获或使用throws关键字声明抛出,程序也能编译通过。运行时
异常一般是由程序中的逻辑错误引起的,在程序运行时无法恢复。比如通过数
组的角标访问数组的元素时,如果超过了数组的最大角标,就会发生运行时异
常,代码如下所示:
上面代码中,由于数组arr的length为5,最大角标应为4,当使用arr[6]访问数组中的元素就会发生数组角标越界的异常。
自定义异常
JDK中定义了大量的异常类,虽然这些异常类可以描述编程时出现的大部分异常情况,但是在程序开发中有时可能需要描述程序中特有的异常情况,在Java中允许用户自定义异常,但自定义的异常类必须继承自Exception或其子类
在实际开发中,如果没有特殊的要求,自定义的异常类只需继承Exception类,在构造方法中使用super()语句调用Exception的构造方法即可。
既然自定义了异常,那么该如何使用呢?这时就需要用到throw关键字,throw关键字用于在方法中声明抛出异常的实例对象,其语法格式如下:
package test;
public class test4 extends Exception {
public test4() {
super();
}
public test4(String message) {
super(message);
}
}
package test;
public class test5 {
public static void main(String[] args) {
try {
a(10, 0);
} catch (test4 e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("aa");
}
public static int a(int x, int y) throws test4 {
Integer c = null;
try {
c = x / y;
} catch (Exception e) {
throw new test4("cc");
} finally {
System.out.println("ttt");
}
return c;
}
}