> 文章列表 > 02 捕获所有异常

02 捕获所有异常

02 捕获所有异常

1 捕获所有异常

通过 Exception 捕获所有异常,并调用它从其基类 Throwable 继承的方法进一步了解当然异常的类型。

package com.hcong.exceptions;/* @Classname ExceptionMethods* @Date 2023/4/15 16:45* @Created by HCong*/
public class ExceptionMethods {public static void main(String[] args) {try {throw new NullPointerException("My Exception");} catch (Exception e) {System.out.println("Caught Exception");System.out.println("getMessage():" + e.getMessage());System.out.println("getLocalizedMessage():" + e.getLocalizedMessage());System.out.println("toString():" + e);System.out.print("printStackTrace():");e.printStackTrace(System.out);}}
}
Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.NullPointerException: My Exception
printStackTrace():java.lang.NullPointerException: My Exceptionat com.hcong.exceptions.ExceptionMethods.main(ExceptionMethods.java:11)

2 栈轨迹

printStackTrace() 方法所提供的信息可以通过 getStackTrace() 方法来直接访问,这个方法将返回一个由栈轨迹中的元素所构成的数组,其中每一个元素都表示栈中的一桢。

package com.hcong.exceptions;/* @Classname WhoCalled* @Date 2023/4/15 16:50* @Created by HCong*/
public class WhoCalled {static void f() {try {throw new Exception();} catch (Exception e) {for (StackTraceElement ste : e.getStackTrace()) {System.out.println(ste.getMethodName());}}}static void g() {f();}static void h() {g();}public static void main(String[] args) {f();System.out.println("=================");g();System.out.println("=================");h();}
}
f
main
=================
f
g
main
=================
f
g
h
main

元素 0 是栈顶元素,并且是调用序列中的最后一个方法调用(这个 Throwable 被创建和抛出之处)。数组中的最后一个元素和栈底是调用序列中的第一个方法调用。

3 重新抛出异常

将捕获的异常重新抛出,重抛异常会把异常抛给上一级环境中的异常处理程序,同一个 try 块的后续 catch 子句将被忽略。此外,值得注意的是:

  • 如果只是把当前异常对象重新抛出,那么 printStackTrace()
    方法显示的将是原来异常抛出点的调用栈信息,而并非重新抛出点的信息
  • 要想更新这个信息,可以调用 fillInStackTrace() 方法,这将返回一个Throwable 对象,它是通过把当前调用栈信息填入原来那个异常对象而建立的。
package com.hcong.exceptions;/* @Classname Rethrowing* @Date 2023/4/15 17:05* @Created by HCong*/
public class Rethrowing {public static void f() throws Exception {System.out.println("originating the exception in f()");throw new Exception("thrown from f()");}public static void g() throws Exception {try {f();} catch (Exception e) {System.out.println("Inside g(), e.printStackTrace()");e.printStackTrace(System.out);throw e;}}public static void h() throws Exception {try {f();} catch (Exception e) {System.out.println("Inside h(), e.printStackTrace()");e.printStackTrace(System.out);throw (Exception) e.fillInStackTrace();}}public static void main(String[] args) {try {g();} catch (Exception e) {System.out.println("main: printStackTrace()");e.printStackTrace(System.out);  // 栈轨迹与 g() 中的一致}System.out.println("=============================================");try {h();} catch (Exception e) {System.out.println("main: printStackTrace()");e.printStackTrace(System.out);  // 栈轨迹与 h() 中的不一致}}
}
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()at com.hcong.exceptions.Rethrowing.f(Rethrowing.java:12)at com.hcong.exceptions.Rethrowing.g(Rethrowing.java:17)at com.hcong.exceptions.Rethrowing.main(Rethrowing.java:39)
main: printStackTrace()
java.lang.Exception: thrown from f()at com.hcong.exceptions.Rethrowing.f(Rethrowing.java:12)at com.hcong.exceptions.Rethrowing.g(Rethrowing.java:17)at com.hcong.exceptions.Rethrowing.main(Rethrowing.java:39)
=============================================
originating the exception in f()
Inside h(), e.printStackTrace()
java.lang.Exception: thrown from f()at com.hcong.exceptions.Rethrowing.f(Rethrowing.java:12)at com.hcong.exceptions.Rethrowing.h(Rethrowing.java:28)at com.hcong.exceptions.Rethrowing.main(Rethrowing.java:46)
main: printStackTrace()
java.lang.Exception: thrown from f()at com.hcong.exceptions.Rethrowing.h(Rethrowing.java:33)at com.hcong.exceptions.Rethrowing.main(Rethrowing.java:46)Process finished with exit code 0