前言
Java异常分为两大类:checked exceptions 和 unchecked exceptions。在本文中,我们将提供一些关于如何使用它们的代码示例。
checked exceptions (Java在编译阶段处理)
可检查异常 在源代码里必须显式地进行捕获处理,这是编译期检查的一部分。
比如,我们应该使用throw关键字来声明checked exception。
private static void checkedExceptionWithThrows() throws FileNotFoundException {
File file = new File("not_existing_file.txt");
FileInputStream stream = new FileInputStream(file);
}
我们也可以使用try-catch来处理 checked exception:
private static void checkedExceptionWithTryCatch() {
File file = new File("not_existing_file.txt");
try {
FileInputStream stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Java中常见的checked exception有IOException、SQLException和ParseException。exception是checked exception的父类。因此,我们可以通过继承exception来自定义checked exception。
Unchecked Exceptions(Java不会在编译时检查unchecked exceptions)
不检查异常就是所谓的 运行时异常 ,类似 NullPointerException、ArrayIndexOutOfBoundsException 之类,通常是可以编码避免的逻辑错误,具体根据需要来判断是否需要捕获,并不会在编译期强制要求。
例如,如果我们将一个数字除以0,Java将抛出 ArithmeticException。
private static void divideByZero() {
int numerator = 1;
int denominator = 0;
int result = numerator / denominator;
}
更重要的是,我们不必在一个带有throws 关键字的方法中声明unchecked exceptions。虽然上面的代码在编译时没有任何错误,但它会在运行时抛出ArithmeticException 。
@GetMapping("/test")
public String test() {
throw new RuntimeException("报错啦");
}
Java中一些常见的unchecked exceptions 是NullPointerException、ArrayIndexOutOfBoundsException和IllegalArgumentException。
何时使用checked exception和unchecked exception
在Java中使用异常是一个很好的实践,这样我们就可以将错误处理代码从常规代码中分离出来。但是,我们需要决定抛出哪种类型的异常。Oracle Java文档提供了关于何时使用checked exception和unchecked exception的建议。
“If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”
如果程序在获取异常信息后,并能够从异常中恢复,则使用checked exception;如果程序在获取异常信息后依旧无法从异常中恢复,则将其设置为unchecked exception。
例如,在打开文件之前,我们可以首先验证输入的文件名。如果用户输入的文件名无效,我们可以抛出一个自定义checked exception。
if (!isCorrectFileName(fileName)) {
throw new IncorrectFileNameException("Incorrect filename : " + fileName );
}
通过这种方式,我们可以通过接受用户重新输入文件名来恢复系统。
再比如,客户端发送请求到数据库查询某条记录,而这条记录有可能找到,也有可能没找到。这时候抛出unchecked exception就是合适的。
异常处理对程序性能的影响性能问题
// 使用 com.alibaba.fastjson
JSONArray array = new JSONArray();
String jsonStr = "{'name':'laowang'}";
try {
array = JSONArray.parseArray(jsonStr);
} catch (Exception e) {
array.add(JSONObject.parse(jsonStr));
}
System.out.println(array.size());
我们从性能角度来审视一下 Java 的异常处理机制,这里有两个可能会相对昂贵的地方:
-
try-catch代码段会产生额外的性能开销,或者换个角度说,它往往会影响 JVM 对代码进行优化,所以建议仅捕获有必要的代码段,尽量不要一个大的try包住整段的代码;与此同时,利用异常控制代码流程,也不是一个好主意,远比我们通常意义上的条件语句(if/else、switch)要低效。 - Java 每实例化一个
Exception,都会对当时的栈进行快照,这是一个相对比较重的操作。如果发生的非常频繁,这个开销可就不能被忽略了。
以上使用 try-catch 处理业务的代码,可以修改为下列代码:
// 使用 com.alibaba.fastjson
JSONArray array = new JSONArray();
String jsonStr = "{'name':'laowang'}";
if (null != jsonStr && !jsonStr.equals("")) {
String firstChar = jsonStr.substring(0, 1);
if (firstChar.equals("{")) {
array.add(JSONObject.parse(jsonStr));
} else if (firstChar.equals("[")) {
array = JSONArray.parseArray(jsonStr);
}
}
System.out.println(array.size());