一、异常处理
1. try-catch-finally
try {
// 可能出问题的代码
int result = 10 / 0;
System.out.println("这行不会执行");
} catch (ArithmeticException e) {
// 捕获特定异常
System.out.println("除数不能为0: " + e.getMessage());
} catch (Exception e) {
// 捕获通用异常(范围从窄到宽)
System.out.println("其他异常: " + e.getMessage());
} finally {
// 始终执行(类似 JS 的 finally)
System.out.println("清理资源");
}
大约 4 分钟
