1. Java 异常类的层次结构

图片摘自 Simple Snippets

从图中可以看出, Exceptions 类和 Errors 类都继承于 Throwable 类。


2. Java Exception

直接上源码(摘自 JDK 11.0.10 )

public class Exception extends Throwable {
static final long serialVersionUID = -3387516993124229948L;

/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public Exception() {
super();
}

/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public Exception(String message) {
super(message);
}

/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(String message, Throwable cause) {
super(message, cause);
}

/**
* Constructs a new exception with the specified cause and a detail
* message of {@code (cause==null ? null : cause.toString())} (which
* typically contains the class and detail message of {@code cause}).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public Exception(Throwable cause) {
super(cause);
}

/**
* Constructs a new exception with the specified detail message,
* cause, suppression enabled or disabled, and writable stack
* trace enabled or disabled.
*
* @param message the detail message.
* @param cause the cause. (A {@code null} value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param enableSuppression whether or not suppression is enabled
* or disabled
* @param writableStackTrace whether or not the stack trace should
* be writable
* @since 1.7
*/
protected Exception(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

​ 在 Exception(异常) 类中,可以分为 Check ExceptionsUncheck Exceptions(Runtime Exception) 。异常可由程序自行处理。其中检查异常是在程序编写过程中必须处理的(不处理无法通过编译),不受检查异常在编译过程中无法发现。

2.1 Check Exceptions

​ 这类异常必须由 try / catch 进行捕获,如果不进行捕获,则不能通过编译。常见的 Check Exceptions有: IOException / ClassNotFoundExcepiton / SQLException / …

2.2 Uncheck Exceptions

RuntimException及其子类都属于 Uncheck Exception。常见的 Uncheck Exception 有:NullPointerExceptionNumberFormatException(字符串转换为数字)ArrayIndexOutOfBoundsException(数组越界)ClassCastException(类型转换错误)ArithmeticException(算术错误)


3. Errors

ErrorError属于程序无法处理的错误,这类错误无法用 catch来进行捕获。比如 JVM 虚拟机错误 / JVM 虚拟机内存不足 / 类定义错误 / … 当发生这种异常时, JVM 一般会线程终止。


4. try - catch -finall

  • try: 用于执行可能出现异常的代码块,后面会跟着 [0-n]catch
  • catch: 用于处理 try块中捕获到的异常
  • finally: 用于之后的工作,不论是否抛出异常,finally块内的代码都会被执行

特殊情况(finally 块不执行): – 参考 Java Guide Blog

  • tryfinally 块中用了 System.exit(int)退出程序。如果 System.exit(int) 在异常语句之后,finally 还是会被执行

  • 程序所在的线程死亡

  • CPU 关闭


5. try - with - resources

​ 相较于 try - catch 语句 try - with - resources主要用于关闭资源(即实现 java.lang.AutoCloseable或者 java.io.Closeable 的对象)
​ 如果有多个资源需要关闭时,多个资源之间需要使用 分号 分隔。