Java在使用try-catch-finally时,如果在finally中也包含try-catch时, 如果第一个catch到异常后抛出, 由于finally不管是否出现异常都会执行, 这时在执行finally代码块时,如果也出现异常抛出, 程序只能捕获到第二次出现的异常, 而第一个异常就会被覆盖, 从而无法捕获到错误信息, 例如:
public class ExceptionHandler {
public static void main(String[] args) {
new ExceptionHandler().occurException();
}
public void occurException() {
FileInputStream input = null;
try {
input = new FileInputStream(new File("xxxx"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上代码会正常打印出两个异常: FileNotFoundException 和 java.lang.NullPointerException
java.io.FileNotFoundException: xxxx (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at ExceptionHandler.occurException(ExceptionHandler.java:19)
at ExceptionHandler.main(ExceptionHandler.java:13)
Exception in thread "main" java.lang.NullPointerException
at ExceptionHandler.occurException(ExceptionHandler.java:24)
at ExceptionHandler.main(ExceptionHandler.java:13)
但是在实际项目中异常不会直接printStackTrace而是抛出, 由调用者来决定要做什么处理, 并且实际项目中会自定义一个异常类来统一处理异常, 如下:
- ApplicationException.java
public class ApplicationException extends RuntimeException {
private static final long serialVersionUID = 8328325784637321672L;
public ApplicationException() {
super();
}
public ApplicationException(String message, Throwable cause) {
super(message, cause);
}
public ApplicationException(String message) {
super(message);
}
public ApplicationException(Throwable cause) {
super(cause);
}
}
- ExceptionHandler.java
public class ExceptionHandler {
public static void main(String[] args) {
try {
new ExceptionHandler().occurException();
} catch (ApplicationException e) {
e.printStackTrace();
}
}
public void occurException() throws ApplicationException {
FileInputStream input = null;
try {
input = new FileInputStream(new File("xxxx"));
} catch (FileNotFoundException e) {
throw new ApplicationException(e);
} finally {
try {
input.close();
} catch (Exception e) {
throw new ApplicationException(e);
}
}
}
}
上面代码好像很正常, 但是这时候就会出现异常信息丢失的现象, main 方法只能捕获到 java.lang.NullPointerException.
ApplicationException: java.lang.NullPointerException
at ExceptionHandler.occurException(ExceptionHandler.java:25)
at ExceptionHandler.main(ExceptionHandler.java:9)
Caused by: java.lang.NullPointerException
at ExceptionHandler.occurException(ExceptionHandler.java:23)
... 1 more
正确的异常处理方式为
public class ExceptionHandler {
public static void main(String[] args) {
try {
new ExceptionHandler().occurException();
} catch (ApplicationException e) {
e.printStackTrace();
}
}
public void occurException() throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File("xxxx"));
} catch (FileNotFoundException e) {
processException = e;
} finally {
try {
input.close();
} catch (Exception e) {
if(null == processException){
throw new ApplicationException(e);
}else{
throw new ApplicationException("FileInputStream close exception", processException);
}
}
if(processException !=null){
throw new ApplicationException(processException);
}
}
}
}
异常信息为
ApplicationException: FileInputStream close exception
at ExceptionHandler.occurException(ExceptionHandler.java:28)
at ExceptionHandler.main(ExceptionHandler.java:9)
Caused by: java.io.FileNotFoundException: xxxx (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at ExceptionHandler.occurException(ExceptionHandler.java:18)
... 1 more
上面只是举列子来说明异常信息被覆盖的现象以及处理方式, 实际上finally中会对input做一个非空判断, 所以input.close 只会出现IOException, 但是问题还是一样, 一旦close 出现异常, 还是会有可能覆盖前一个异常.
实际写法:
public class ExceptionHandler {
public static void main(String[] args) {
try {
new ExceptionHandler().occurException();
} catch (ApplicationException e) {
e.printStackTrace();
}
}
public void occurException() throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File("xxxx"));
input.read();
} catch (IOException e) {
processException = e;
} finally {
try {
if(null != input){
input.close();
}
} catch (IOException e) {
if(null == processException){
throw new ApplicationException(e);
}else{
throw new ApplicationException("FileInputStream close exception", processException);
}
}
if(processException !=null){
throw new ApplicationException(processException);
}
}
}
}
这样代码是不是很凌乱, 实际上在occurException方法中你只是关注下面两行代码, 其他的代码在每个读取流的地方都是一样的, 繁琐重复, 严重违反例如DRY( don’t repeat yourself)原则, 我们可以采用模板方法, 定义一个模板, 把实际处理方法定义为抽象方法处理, 详见: 异常管理 - 采用模板方法优化try-catch-finally
input = new FileInputStream(new File("xxxx"));
input.read();
来源: IT艺术博客(http://www.itart.cn)