了解异常
异常
在计算机程序运行的过程中,总是会出现各种各样的错误。
有一些错误是用户造成的,比如,希望用户输入一个int类型的年龄,但是用户的输入是abc:
// 假设用户输入了abc:
String s = "abc";
int n = Integer.parseInt(s); // NumberFormatException!
程序想要读写某个文件的内容,但是用户已经把它删除了:
// 用户删除了该文件:
String t = readFile("C:\\abc.txt"); // FileNotFoundException!
还有一些错误是随机出现,并且永远不可能避免的。比如:
- 网络突然断了,连接不到远程服务器;
- 内存耗尽,程序崩溃了;
- 用户点“打印”,但根本没有打印机;
- ……
一个健壮的程序必须处理各种各样的错误。错误就是程序调用某个函数的时候,如果失败了,就表示出错。调用方如何获知调用失败的信息?有两种方法:
1)方法一:约定返回错误码
例如,处理一个文件,如果返回0,表示成功,返回其他整数,表示约定的错误码:
int code = processFile("C:\\test.txt");
if (code == 0) {
// ok:
} else {
// error:
switch (code) {
case 1:
// file not found:
case 2:
// no read permission:
default:
// unknown error:
}
}
2)方法二:在语言层面上提供一个异常处理机制
Java内置了一套异常处理机制,总是使用异常来表示错误。
异常是一种class,因此它本身带有类型信息。异常可以在任何地方抛出,但只需要在上层捕获,这样就和方法调用分离了:
try {
String s = processFile(“C:\\test.txt”);
// ok:
} catch (FileNotFoundException e) {
// file not found:
} catch (SecurityException e) {
// no read permission:
} catch (IOException e) {
// io error:
} catch (Exception e) {
// other error:
}
异常class
异常分为运行时异常(RuntimeException)、受检异常(Checked Exception)、系统错误(Error)。
Java的异常是class,它的继承关系如下:
┌───────────┐
│ Object │
└───────────┘
▲
│
┌───────────┐
│ Throwable │
└───────────┘
▲
┌─────────┴─────────┐
│ │
┌───────────┐ ┌───────────┐
│ Error │ │ Exception │
└───────────┘ └───────────┘
▲ ▲
┌───────┘ ┌────┴──────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐┌──────────────────┐
│OutOfMemoryError │... │RuntimeException ││Checked Exception │
└─────────────────┘ └─────────────────┘└──────────────────┘
从继承关系可知:Throwable
是异常体系的根,它继承自Object
。Throwable
有两个体系:Error
和Exception
。
Error
是无需捕获的严重错误(比如OutOfMemoryError、NoClassDefFoundError、StackOverflowError),因为程序一般对错误无能为力,所以捕获了也处理不了。
Exception
可以分为两类,RuntimeException是一类(也称为Unchecked Exception),非RuntimeException的其他异常都称为Checked Exception)。
RuntimeException不需要捕获,Checked Exception必须捕获
Exception
│
├─ RuntimeException
│ │
│ ├─ NullPointerException
│ │
│ ├─ IndexOutOfBoundsException
│ │
│ ├─ SecurityException
│ │
│ └─ IllegalArgumentException
│ │
│ └─ NumberFormatException
│
├─ IOException
│ │
│ ├─ UnsupportedCharsetException
│ │
│ ├─ FileNotFoundException
│ │
│ └─ SocketException
│
├─ ParseException
│
├─ GeneralSecurityException
│
├─ SQLException
│
└─ TimeoutException
1)RuntimeException不需要捕获
public static void main(String[] args) {
int[] numbers={1,2,3};
int sum=numbers[0]+numbers[3];
}
上面例子可以编译,但是运行会报异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
因为数组numbers的成员是从0到2,所以int sum=numbers[0]+numbers[3]
这句代码访问不存在的数组元素是访问不到的。这类由于程序员自身原因导致的错误,JVM编译阶段不能发现,运行时才会出抛出的错误,就是RuntimeException。对于这种异常,无法在运行过程中处理,我们唯一的处理办法就是修改程序代码然后重新编译运行(例子中应该修改为int sum=numbers[0]+numbers[2]
),所以不需要捕获异常。
2)Checked Exception必须捕获
public void method(String filepath){
File f = new File(filepath);
FileReader r = new FileReader(f); // Unhandled exception: java.io.FileNotFoundException
}
这段代码在编译阶段不能通过,当然无法运行,这是Checked Exception。这种并非由于程序员本身逻辑代码错误引起的,我们可以捕获异常并处理,比如在catch语句块里面提示用户重新输入正确的文件路径等等。
他の者にできたか?ここまでやれたか?この先できるか?いいや、仆にしかできない!