Exception 分为两类: 运行时异常(非受检异常)继承自 RuntimeException, 编译器不强制处理,多为代码逻辑错误导致。常见例子: NullPointerException(空指针异常) ArrayIndexOutOfBoundsException(数组下标越界) ArithmeticException(算术运算异常,如除 0) ClassCastException(类型转换异常) IllegalArgumentException(非法参数异常) 非运行时异常(受检异常)除 RuntimeException 及其子类外的 Exception, 编译器强制要求处理(try-catch 或 throws),多为外部环境问题。常见例子: IOException(文件 / 网络读写异常) SQLException(数据库操作异常) ClassNotFoundException(类未找到) CloneNotSupportedException(克隆不支持)package further.zwf; /** * 八股文异常问题 * * @author ZengWenFeng * @date 2023.11.17 * @mobile 13805029595 * @email 117791303@QQ.com */ public class FinallyTest { public static void main(String[] args) { System.out.println("===== 场景1:try 里调用 System.exit(0) 终止 JVM,finally 不执行 ====="); try { System.out.println("执行 try 代码"); System.exit(0); // 直接退出 JVM,程序终止 } finally { System.out.println("执行 finally 代码"); // 这行永远不会输出 } } // 下面是另外 3 种 finally 不执行的场景(注释掉上面 main 方法即可运行) /* public static void main(String[] args) { // 场景2:try 内死循环,永远到不了 finally System.out.println("===== 场景2:死循环,finally 不执行 ====="); try { while (true) { // 死循环 } } finally { System.out.println("执行 finally"); // 永远不执行 } } */ /* public static void main(String[] args) { // 场景3:JVM 崩溃/硬件故障(模拟:致命错误) // 实际开发中是系统崩溃、断电等 System.out.println("===== 场景3:JVM 崩溃,finally 不执行 ====="); try { throw new Error("JVM 级错误"); } finally { System.out.println("执行 finally"); // 不会执行 } } */ /* public static void main(String[] args) { // 场景4:线程被强制杀死(不推荐使用,但能说明问题) System.out.println("===== 场景4:线程被杀死,finally 不执行 ====="); Thread t = new Thread(() -> { try { Thread.sleep(1000); } catch (Exception e) { } finally { System.out.println("执行 finally"); // 不会执行 } }); t.start(); t.stop(); // 强制杀死线程 } */ }我用直白一点的话总结下吧: err 程序直接就无法运行,中断了; exception,程序还可以继续执行