Java中的异常及其使用

x33g5p2x  于2021-11-25 转载在 Java  
字(9.1k)|赞(0)|评价(0)|浏览(357)

一、认识异常

什么叫异常?异常就是非同寻常,对正确的代码会程序报错误。如果不使用异常,程序还会自动终止。

我们常见的异常有:

算术异常:ArithmeticException

System.out.println(10 / 0);
Exception in thread "main" java.lang.ArithmeticException: / by zero

数组下标越界:超出索引范围:ArrayIndexOutOfBoundsException

int[] arr = {1, 2, 3};
System.out.println(arr[100]);
// 执行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

对空指针访问:NullPointerException

public class Test {
    public int num = 10;
    public static void main(String[] args) {
        Test t = null;
        System.out.println(t.num);
   }
}
// 执行结果
Exception in thread "main" java.lang.NullPointerException

除了上面这几种最常见的异常,还有很多,此处不再举例。

异常分为编译时异常(受查异常)和运行时异常(非受查异常)。

编译时异常就是编译时期程序会报错,此处需要程序员自己去更改代码程序,例如将 System.out.println 拼写错了, 写成了 system.out.println. 此时编译过程中就会出
错, 这是 “编译期” 出错。

运行时异常是运行时报错,运行时已经编译,产生.class文件交给JVM处理。因此是在JVM处报错的。

异常的种类有很多, 不同种类的异常具有不同的含义, 也有不同的处理方式。

二、异常的用法

1.捕捉异常

try{ 
 有可能出现异常的语句 ; 
}[catch (异常类型 异常对象) {
} ... ]
[finally {
 异常的出口
}]
  • try 代码块中放的是可能出现异常的代码
  • catch 代码块中放的是出现异常后的处理行为
  • finally 代码块中的代码用于处理善后工作, 会在最后执行
  • 其中 catch 和 finally 都可以根据情况选择加或者不加

不处理异常程序会立刻终止,例如:
代码1:

int[] arr = {1, 2, 3};
System.out.println("before");
System.out.println(arr[100]);
System.out.println("after");
// 执行结果
before
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

如果用try catch语句处理异常:
代码2:

int[] arr = {1, 2, 3};
try {
    System.out.println("before");
    System.out.println(arr[100]);
    System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
    // 打印出现异常的调用栈
    e.printStackTrace();
}
System.out.println("after try catch");
// 执行结果
before
java.lang.ArrayIndexOutOfBoundsException: 100
 at demo02.Test.main(Test.java:10)
after try catch

可见,如果此处用try catch语句处理可能会出现异常的语句,即使程序会报错,但是我们处理异常后依然会运行后面的代码。

catch 只能处理对应种类的异常
假设我们修改了代码, 让代码抛出的是空指针异常:
代码3:

int[] arr = {1, 2, 3};
try {
    System.out.println("before");
    arr = null;
    System.out.println(arr[100]);
    System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
    e.printStackTrace();
}
System.out.println("after try catch");
// 执行结果
before
Exception in thread "main" java.lang.NullPointerException
 at demo02.Test.main(Test.java:11)

此时, catch 语句不能捕获到刚才的空指针异常,因为异常类型不匹配。因为没有捕捉到异常,因此后面的语句也不会被执行。

catch捕捉的异常可以有多个。
代码4:

int[] arr = {1, 2, 3};
try {
    System.out.println("before");
    arr = null;
    System.out.println(arr[100]);
    System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("这是个数组下标越界异常");
    e.printStackTrace();
} catch (NullPointerException e) {
    System.out.println("这是个空指针异常");
    e.printStackTrace();
}
System.out.println("after try catch");
// 执行结果
before
这是个空指针异常
java.lang.NullPointerException
 at demo02.Test.main(Test.java:12)
after try catch

一段代码可能会抛出多种不同的异常, 不同的异常有不同的处理方式,因此可以搭配多个 catch 代码块,如果多个异常的处理方式是完全相同, 也可以写成这样。两个异常中间用|分开,定义的是同一个变量。

catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
 ...
}

也可以用一个 catch 捕获Exception中的所有异常(不推荐),Exception异常是所有异常的父类。因为如果漏写了e.printStackTrace();就不能确定出现的是哪一种异常,当代码过多时就可以出现不必要的麻烦。
代码5:

int[] arr = {1, 2, 3};
try {
    System.out.println("before");
    arr = null;
    System.out.println(arr[100]);
    System.out.println("after");
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println("after try catch");
// 执行结果
before
java.lang.NullPointerException
 at demo02.Test.main(Test.java:12)
after try catch

finally 表示最后的善后工作, 例如释放资源。
代码6:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try{
            int n = scanner.nextInt();
        }catch(InputMismatchException e) {
            e.printStackTrace();
        }finally {
            scanner.close();
        }
    }

但是像上面这样写时编译器会在try处报一个警告,提示你写为下面这种方式。因此try也有负责回收资源的作用。它能够保证在 try 执行完毕后自动调用 Scanner
的 close 方法。
代码7:

public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
        } catch (InputMismatchException e) {
            e.printStackTrace();
        }
    }

如果本方法中没有合适的处理异常的方式, 就会沿着调用栈向上传递。即如果在一个方法内部出现了异常,假设没有处理该异常,则交给调用该方法的方法去处理异常。以此类推…如果向上一直传递都没有合适的方法处理异常, 最终就会交给 JVM 处理, 程序就会异常终止(和我们最开始未使用 try catch 时是一样的)
代码8:

public static void main(String[] args) {
    try {
        func();
   } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
   }
    System.out.println("after try catch");
}
public static void func() {
    int[] arr = {1, 2, 3};
    System.out.println(arr[100]);
}
// 直接结果
java.lang.ArrayIndexOutOfBoundsException: 100
 at demo02.Test.func(Test.java:18)
 at demo02.Test.main(Test.java:9)
after try catch

代码9:

public static void main(String[] args) {
    func();
    System.out.println("after try catch");
}
public static void func() {
    int[] arr = {1, 2, 3};
    System.out.println(arr[100]);
}
// 执行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
 at demo02.Test.func(Test.java:14)
 at demo02.Test.main(Test.java:8)

可以看到, 程序已经异常终止了, 没有执行到 System.out.println(“after try catch”); 这一行。

2.异常的处理流程

  1. 程序先执行 try 中的代码
  2. 如果 try 中的代码出现异常, 就会结束 try 中的代码, 看和 catch 中的异常类型是否匹配.
  3. 如果找到匹配的异常类型, 就会执行 catch 中的代码
  4. 如果没有找到匹配的异常类型, 就会将异常向上传递到上层调用者
  5. 无论是否找到匹配的异常类型, finally 中的代码都会被执行到(在该方法结束之前执行)
  6. 如果上层调用者也没有处理的了异常, 就继续向上传递
  7. 一直到 main 方法也没有合适的代码处理异常, 就会交给 JVM 来进行处理, 此时程序就会异常终止

3.抛出异常

除了 Java 内置的类会抛出一些异常之外,使用 throw 关键字完成这个操作,一般来说throw与throws一起使用。注:throw与throws是两个不同的关键字。

public static void main(String[] args) { 
 System.out.println(divide(10, 0)); 
} 
public static int divide(int x, int y) { 
 if (y == 0) { 
 throw new ArithmeticException("抛出除 0 异常"); 
 } 
 return x / y; 
} 
// 执行结果
Exception in thread "main" java.lang.ArithmeticException: 抛出除 0 异常
 at demo02.Test.divide(Test.java:14) 
 at demo02.Test.main(Test.java:9)

在这个代码中, 我们可以根据实际情况来抛出需要的异常. 在构造异常对象同时可以指定一些描述性信息。假设抛出异常后调用该方法的方法没有处理异常,直到main方法仍没有处理异常,则还是交给JVM处理。

4.异常说明

我们在处理异常的时候, 通常希望知道这段代码中究竟会出现哪些可能的异常。
我们可以使用 throws 关键字, 把可能抛出的异常显式的标注在方法定义的位置,从而提醒调用者要注意捕获这些异常。

public static int divide(int x, int y) throws ArithmeticException { 
 if (y == 0) { 
   throw new ArithmeticException("抛出除 0 异常"); 
 } 
 return x / y; 
}

5.关于 finally 的注意事项

finally 中的代码保证一定会执行到. 这也会带来一些麻烦。

public static void main(String[] args) { 
 System.out.println(func()); 
} 
public static int func() { 
 try { 
 return 10; 
 } finally { 
 return 20; 
 } 
} 
// 执行结果
20

注:finally 执行的时机是在方法返回之前(try 或者 catch 中如果有 return 会在这个 return 之前执行 finally),但是如果finally 中也存在 return 语句, 那么就会执行 finally 中的 return, 从而不会执行到 try 中原有的 return。但我们一般不在finally处return。

三、异常体系

这张图是不全的,因为Exception的子类有很多,如下图:

  • 顶层类 Throwable 派生出两个重要的子类, Error 和 Exception
  • 其中 Error 指的是 Java 运行时内部错误和资源耗尽错误,应用程序不抛出此类异常,这种内部错误一旦出现,除了告知用户并使程序终止之外, 再无能无力。
  • Exception 是我们程序员所使用的异常类的父类,其中 Exception 有一个子类称为 RuntimeException , 这里面又派生出很多我们常见的异常类NullPointerException , IndexOutOfBoundsException 等

Error错误示例:

public static void func() {
        func();
    }
    public static void main(String[] args) {
        func();
    }

四、受查异常与非受查异常

Java语言规范将派生于 Error 类或 RuntimeException 类的所有异常称为非受查异常, 所有的其他异常称为受查异常。如果一段代码可能抛出 受查异常, 那么必须显式进行处理

public static void main(String[] args) { 
 System.out.println(readFile()); 
} 
public static String readFile() { 
 // 尝试打开文件, 并读其中的一行. 
 File file = new File("d:/test.txt"); 
 // 使用文件对象构造 Scanner 对象. 
 Scanner sc = new Scanner(file); 
 return sc.nextLine(); 
}
// 编译出错
Error:(13, 22) java: 未报告的异常错误java.io.FileNotFoundException; 必须对其进行捕获或声明以便抛出

如 FileNotFoundException 这样的异常就是受查异常,如果不显式处理, 编译无法通过,显式处理的方式有两种:

1.使用 try catch 包裹起来
因为在输入给sc时可能会出错,因此可以用try catch捕捉,此处虽然运行时可能会报错,但是会编译通过。这就是处理异常的好处。

public static void main(String[] args) { 
 System.out.println(readFile()); 
} 
public static String readFile() { 
 File file = new File("d:/test.txt"); 
 Scanner sc = null; 
 try { 
 sc = new Scanner(file); 
 } catch (FileNotFoundException e) { 
 e.printStackTrace(); 
 } 
 return sc.nextLine(); 
}

2.在方法上加上异常说明, 相当于将处理动作交给上级调用者
使用throws说明异常,则不需要在readFile方法内部使用try catch捕捉异常。如果main方法没有使用try catch捕捉异常则交给JVM处理,否则是自己捕捉异常。

public static void main(String[] args) { 
 try { 
 System.out.println(readFile()); 
 } catch (FileNotFoundException e) { 
 e.printStackTrace(); 
 } 
} 
public static String readFile() throws FileNotFoundException { 
 File file = new File("d:/test.txt"); 
 Scanner sc = new Scanner(file); 
 return sc.nextLine(); 
}

五、自定义异常类

Java 中虽然已经内置了丰富的异常类, 但是我们实际场景中可能还有一些情况需要我们对异常类进行扩展, 创建符合我们实际情况的异常,当不符合时就让它编译时期或者运行时期就报错。

自定义异常类继承Exception类默认是受查异常,继承RuntimeException类默认是非受查异常。

此处一个异常类继承Exception还是RuntimeException的意义:
我们来举个例子:
假设自定义异常类继承的都是RuntimeException,则此处编译时不会报错,因为继承RuntimeException是非受查异常,则在运行时才会报错。
代码1:

class NameException extends RuntimeException {
    public NameException(String name) {
        super(name);
    }
}
class PassWordException extends RuntimeException {
    public PassWordException(String message) {
        super(message);
    }
}
public class Test {
    private static String name = "bit";
    private static String password = "1234";

    public static void login(String name, String password) throws NameException, PassWordException {
        if (!Test.name.equals(name)) {
            throw new NameException("名字错误");
        }
        if (!Test.password.equals(password)) {
            throw new PassWordException("密码错误");
        }
    }

    public static void main(String[] args) {
        login("bit", "123");
    }
}

假设自定义异常类继承的都是Exception,则默认自定义异常类为受查异常,再用上面的代码,只将自定义继承改为Exception。则编译时期就会报错。

class NameException extends Exception {
    public NameException(String name) {
        super(name);
    }
}
class PassWordException extends Exception {
    public PassWordException(String message) {
        super(message);
    }
}
public class Test {
    private static String name = "bit";
    private static String password = "1234";

    public static void login(String name, String password) throws NameException, PassWordException {
        if (!Test.name.equals(name)) {
            throw new NameException("名字错误");
        }
        if (!Test.password.equals(password)) {
            throw new PassWordException("密码错误");
        }
    }

    public static void main(String[] args) {
        login("bit", "123");
    }
}

因为受查异常要用两种显示的方式进行处理,要不用try catch语句处理,要不抛出异常用throw和throws成对处理。

对于为何自定义一个异常类需要在构造方法中调用父类的构造方法。是因为我们可以在throw new NameException();时能够在括号中说明错的原因。而输入错的原因时又是一个字符串。我们在看Exception异常类的源码时,可以看到它也是继承了Throwable类。而其中也调用了构造方法。我们也已经知道了一个子类的构造方法是默认一个不带参数的调用父类的构造方法的。因此一切都能说通了。

相关文章