Java异常概述 #
一、异常概述 #
异常是程序执行过程中发生的不正常事件,会中断正常的指令流程。
1.1 异常的作用 #
- 提高程序健壮性
- 分离正常代码和错误处理代码
- 提供错误信息
1.2 异常体系 #
text
Throwable
├── Error(错误)
│ ├── VirtualMachineError
│ ├── OutOfMemoryError
│ └── StackOverflowError
└── Exception(异常)
├── RuntimeException(运行时异常)
│ ├── NullPointerException
│ ├── ArrayIndexOutOfBoundsException
│ ├── ArithmeticException
│ └── ClassCastException
└── 非RuntimeException(检查异常)
├── IOException
├── SQLException
└── FileNotFoundException
二、Error与Exception #
2.1 Error #
Error是严重错误,程序无法处理。
java
// 栈溢出错误
public void recursiveMethod() {
recursiveMethod(); // 无限递归
}
// 内存溢出错误
public void outOfMemory() {
int[] arr = new int[Integer.MAX_VALUE];
}
2.2 Exception #
Exception是程序可以处理的异常。
java
// 空指针异常
String s = null;
System.out.println(s.length()); // NullPointerException
// 数组越界
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
// 算术异常
int result = 10 / 0; // ArithmeticException
三、异常分类 #
3.1 检查异常(Checked Exception) #
编译时必须处理的异常。
java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public void readFile() {
// 编译错误:必须处理FileNotFoundException
// FileInputStream fis = new FileInputStream("test.txt");
// 正确:处理异常
try {
FileInputStream fis = new FileInputStream("test.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
3.2 非检查异常(Unchecked Exception) #
运行时异常,编译时不强制处理。
java
// RuntimeException及其子类
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
ClassCastException
NumberFormatException
IllegalArgumentException
四、常见异常 #
4.1 NullPointerException #
java
String s = null;
int length = s.length(); // NullPointerException
// 预防
if (s != null) {
int length = s.length();
}
4.2 ArrayIndexOutOfBoundsException #
java
int[] arr = {1, 2, 3};
int num = arr[5]; // ArrayIndexOutOfBoundsException
// 预防
if (index >= 0 && index < arr.length) {
int num = arr[index];
}
4.3 ArithmeticException #
java
int result = 10 / 0; // ArithmeticException
// 预防
int divisor = 0;
if (divisor != 0) {
int result = 10 / divisor;
}
4.4 ClassCastException #
java
Object obj = "Hello";
Integer num = (Integer) obj; // ClassCastException
// 预防
if (obj instanceof Integer) {
Integer num = (Integer) obj;
}
4.5 NumberFormatException #
java
int num = Integer.parseInt("abc"); // NumberFormatException
// 预防
try {
int num = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("数字格式错误");
}
五、异常处理方式 #
5.1 捕获异常 #
java
try {
// 可能抛出异常的代码
} catch (Exception e) {
// 异常处理代码
} finally {
// 无论是否异常都执行
}
5.2 声明异常 #
java
public void readFile() throws IOException {
// 可能抛出IOException的代码
}
5.3 抛出异常 #
java
public void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("年龄不能为负");
}
this.age = age;
}
六、异常处理原则 #
6.1 能处理就处理 #
java
public int divide(int a, int b) {
try {
return a / b;
} catch (ArithmeticException e) {
return 0; // 处理异常,返回默认值
}
}
6.2 不能处理就声明 #
java
public void readFile(String path) throws IOException {
FileInputStream fis = new FileInputStream(path);
// ...
}
6.3 使用具体异常 #
java
// 不推荐
try {
// ...
} catch (Exception e) { // 太宽泛
}
// 推荐
try {
// ...
} catch (FileNotFoundException e) {
// 文件不存在
} catch (IOException e) {
// IO错误
}
七、总结 #
| 类型 | 说明 | 处理要求 |
|---|---|---|
| Error | 严重错误 | 无法处理 |
| 检查异常 | 编译时检查 | 必须处理 |
| 运行时异常 | 运行时发生 | 可选处理 |
异常要点:
- Error是严重错误,程序无法处理
- 检查异常必须处理
- 运行时异常可选处理
- 选择合适的处理方式
最后更新:2026-03-26