try-catch-finally语句
try-catch-finally是Java异常处理的核心语法结构。
基本语法
try-catch结构
try块包裹可能抛出异常的代码,catch块捕获并处理异常。
Java
try {
// 可能抛出异常的代码
int result = 10 / 0;
} catch (ArithmeticException e) {
// 异常处理代码
System.out.println("除数不能为零");
}
try-catch-finally结构
finally块无论是否异常都会执行,用于资源清理。
Java
try {
// 可能异常的代码
FileReader reader = new FileReader("file.txt");
// 读取操作
} catch (IOException e) {
// 处理IO异常
System.out.println("读取失败: " + e.getMessage());
} finally {
// 必定执行,用于关闭资源
System.out.println("清理资源");
}
执行流程
无异常情况
Java
try {
System.out.println("1. try执行");
} catch (Exception e) {
System.out.println("2. catch执行"); // 不执行
} finally {
System.out.println("3. finally执行"); // 执行
}
// 输出: 1. try执行 → 3. finally执行
有异常情况
Java
try {
System.out.println("1. try执行");
int x = 10 / 0; // 抛出异常
} catch (ArithmeticException e) {
System.out.println("2. catch执行"); // 执行
} finally {
System.out.println("3. finally执行"); // 执行
}
// 输出: 1. try执行 → 2. catch执行 → 3. finally执行
异常未捕获情况
Java
try {
System.out.println("1. try执行");
int x = 10 / 0;
} catch (NullPointerException e) {
// 不匹配ArithmeticException
System.out.println("2. catch执行"); // 不执行
} finally {
System.out.println("3. finally执行"); // 执行,然后异常向上抛
}
// 输出: 1. try执行 → 3. finally执行 → 异常继续传播
多个catch块
捕获多种异常
Java
try {
int[] arr = new int[5];
arr[10] = 100; // ArrayIndexOutOfBoundsException
// 或其他操作
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界");
} catch (NullPointerException e) {
System.out.println("空指针");
} catch (Exception e) {
System.out.println("其他异常"); // 兜底处理
}
多个catch块按顺序匹配,子类异常必须在父类异常之前。
catch顺序规则
Java
// 正确顺序:子类在前,父类在后
try {
// ...
} catch (NullPointerException e) { // 子类
// ...
} catch (RuntimeException e) { // 父类
// ...
} catch (Exception e) { // 更高层父类
// ...
}
// 错误顺序:父类在前会覆盖子类
try {
// ...
} catch (Exception e) { // 捕获所有,后面的catch无效
// ...
} catch (NullPointerException e) { // 编译错误:已捕获
// ...
}
finally特性
必定执行
finally块在以下情况都会执行:
- try正常完成
- catch捕获异常
- 异常未捕获向上传播
- try/catch中有return
Java
public String test() {
try {
return "try返回";
} finally {
System.out.println("finally执行"); // 在return之前执行
}
}
// 输出: finally执行 → 返回"try返回"
finally中return
Java
public String test() {
try {
return "try返回";
} finally {
return "finally返回"; // 覆盖try的return
}
}
// 返回"finally返回",慎用
不建议在finally中使用return,会覆盖try/catch的返回值。
finally不执行的情况
- System.exit()强制退出程序
- 线程死亡
- 断电等物理故障
Java
try {
System.exit(0); // 程序终止
} finally {
System.out.println("不执行"); // 不执行
}
异常信息获取
常用方法
Java
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
// 获取异常信息
String message = e.getMessage(); // "by zero"
String toString = e.toString(); // "java.lang.ArithmeticException: / by zero"
e.printStackTrace(); // 打印完整堆栈
}
printStackTrace输出
Java
java.lang.ArithmeticException: / by zero
at Test.main(Test.java:5)
at ...
资源清理模式
传统方式
Java
FileReader reader = null;
try {
reader = new FileReader("file.txt");
// 读取操作
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close(); // 关闭资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java 7+ try-with-resources
text
try (FileReader reader = new FileReader("file.txt")) {
// 读取操作
} catch (IOException e) {
e.printStackTrace();
}
// 自动关闭资源,无需finally
要点总结
- try块包裹可能异常的代码
- catch块捕获并处理异常
- finally块必定执行,用于资源清理
- 多个catch按顺序匹配,子类在前父类在后
- finally在return之前执行
- 不建议在finally中使用return
- System.exit()会导致finally不执行
- e.getMessage()获取异常消息
- e.printStackTrace()打印堆栈信息
- Java 7+推荐try-with-resources自动关闭资源
📝 发现内容有误?点击此处直接编辑