第一部分:异常处理基础
理解异常的概念和Java异常处理的基本结构
学习目标
- 理解异常的概念
- 掌握try-catch-finally的基本语法
- 了解异常的分类
1
异常的概念
异常表示程序运行时出现的错误或异常情况,如文件找不到、网络连接失败等。
public class Test {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // 除以0会抛出ArithmeticException
}
}
上述代码会抛出一个ArithmeticException
异常。
2
try-catch基本语法
public class Test {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // 除以0会抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("发生异常:" + e.getMessage());
}
}
}
上述代码捕获了ArithmeticException
异常,并打印了异常信息。
3
finally块的作用
public class Test {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // 除以0会抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("发生异常:" + e.getMessage());
} finally {
System.out.println("finally块始终执行");
}
}
}
finally
块无论是否捕获异常都会执行,常用于资源清理。
第二部分:异常分类
Java中的异常分为受检查异常和非受检查异常
4
受检查异常
受检查异常是指编译器强制要求处理的异常,如IOException
。
public class FileReader {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("file.txt");
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
}
}
}
5
非受检查异常
非受检查异常是指编译器不要求处理的异常,如ArithmeticException
。
public class Test {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // 除以0会抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("发生异常:" + e.getMessage());
}
}
}
第三部分:异常处理的高级用法
掌握异常处理的高级技巧,提升代码的健壮性
6
捕获多个异常
public class Test {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // 除以0会抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("发生算术异常:" + e.getMessage());
} catch (Exception e) {
System.out.println("发生其他异常:" + e.getMessage());
}
}
}
7
抛出自定义异常
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class Test {
public static void main(String[] args) {
try {
int age = -1;
if (age < 0) {
throw new InvalidAgeException("年龄不能为负数");
}
} catch (InvalidAgeException e) {
System.out.println("发生异常:" + e.getMessage());
}
}
}
第四部分:常见错误与最佳实践
避免异常处理中的常见陷阱
常见错误1:捕获异常后不做处理
try {
// 可能抛出异常的代码
} catch (Exception e) {
// 错误:捕获异常后不做处理
}
捕获异常后应该进行适当的处理,如记录日志或提示用户。
常见错误2:过度使用try-catch
try {
// 可能不会抛出异常的代码
} catch (Exception e) {
// 捕获异常
}
不要在不会抛出异常的代码块中使用try-catch,这会影响代码的可读性和性能。
最佳实践
- 明确捕获异常类型: 尽量捕获具体的异常类型,而不是通用的
Exception
。 - 合理使用finally: 在
finally
块中清理资源。 - 抛出自定义异常: 在合适的情况下抛出自定义异常,使异常信息更明确。
- 记录日志: 捕获异常后记录日志,便于问题排查。
第五部分:实战练习
通过实际项目巩固异常处理知识
练习:文件读取与异常处理
步骤1:定义文件读取方法
public class FileReader {
public static void readFile(String filePath) {
try {
FileReader reader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
} catch (IOException e) {
System.out.println("读取文件时发生异常");
} finally {
System.out.println("文件读取完成");
}
}
}
步骤2:测试文件读取方法
public class Test {
public static void main(String[] args) {
FileReader.readFile("file.txt");
}
}
完成检查清单
- ✅ 定义文件读取方法
- ✅ 捕获文件未找到异常
- ✅ 捕获文件读取异常
- ✅ 在finally块中清理资源
- ✅ 测试文件读取方法