当前位置:   article > 正文

java checkedexception_如何处理Java 中的Checked Exception

suppressing or ignoring the checked exception "e".

想必很多人对JAVA中的Exception不会陌生,但是我们也会碰到诸多的Checked Exception。而我们又不想一层层去捕获,那么就需要想办法来忽略这些Checked Exception。

那么何为Checked Exception, 何为Unchecked Exception。

fe7198e2d635

所示

正如上图中所示:

Checked Exception: 指的是不能恢复,必须要被使用者来处理的一类异常,如果不捕获,那么编译会报错。例如,IOException。

Unchecked Exception: 指的是在运行时才会导致程序奔溃的异常,编译时候并不会报错。例如,Runtime Exception。

如果在代码中处处来处理Checked Exception,那么代码就会变成冗长并且可读性变差,所以在某些情况下需要对其进行处理,变成Unchecked Exception。

1. try catch

最简单的方案之一就是使用try catch然后在捕获到checked exception之后抛出Unchecked Exception。

例如:

public DbConnection getDbConnection(String username, String password) {

try {

return new DbProvider().getConnect(username, password);

} catch (IOException e) {

throw new RuntimeException();

}

}

这样的处理逻辑到处都会用到,代码也会显得冗长,降低了可读性。

2. 一个通用的Wrapper

可以尝试着写一个通用的Wrapper,统一处理类似的Checked Exception。

我们需要把上述代码中的new DbProvider().getConnect(username, password);包装成一个通用的接口RuntimeExceptionWrappable:

public interface RuntimeExceptionWrappable {

T execute() throws IOException;

}

接下来可以替换原有代码中的new DbProvider().getConnect(username, password);:

RuntimeExceptionWrappable wrappable = new RuntimeExceptionWrappable() {

@Override

public DbConnection execute() throws IOException {

return new DbProvider().getConnect(username, password);

}

};

原有代码的逻辑现在就变成下面的形式:

RuntimeExceptionWrappable wrappable = new RuntimeExceptionWrappable() {

@Override

public DbConnection execute() throws IOException {

return new DbProvider().getConnect(username, password);

}

};

try {

return wrappable.execute();

} catch (IOException e) {

throw new RuntimeException();

}

到目前为止,我们依然没有把try catch去掉。我们接着把try catch部分提取出来:

public class RuntimeExceptionWrapper {

public static T wrap(RuntimeExceptionWrappable wrappable) {

try {

return wrappable.execute();

} catch (IOException e) {

throw new RuntimeException();

}

}

}

最后一步,完成整个代码:

RuntimeExceptionWrappable wrappable = new RuntimeExceptionWrappable() {

@Override

public DbConnection execute() throws IOException {

return new DbProvider().getConnect(username, password);

}

};

return RuntimeExceptionWrapper.wrap(wrappable);

到这里就可以看到,Wrapper被抽象到独立的类中了。

3. Stream中的Exception

自从JAVA8依赖,流处理在代码中已经变得越来越常见,这样就不可避免的会有Exception出现在Stream流处理中。

public class UrlHandler {

public List getURLs() {

return Stream.of("http://www.baidu.com", "https://www.google.com")

.map(this:createURL)

.collect(Collectors.toList());

}

private URL createURL(String url) throws MalformedURLException {

return new URL(url);

}

}

上述代码是编译不通过的,原因是createURL抛出了Checked Exception。只有对stream流处理进行修改,接收Exception代码才能编译:

public List getURLs() {

return Stream.of("http://www.baidu.com", "https://www.google.com")

.map(url -> {

try {

return this.createURL(url);

} catch (MalformedURLException e) {

throw new RuntimeException();

}

})

.collect(Collectors.toList());

}

这段代码虽然能够工作,但是依然显得不是很优雅。还是跟前一部分一样,我们抽取出来一个通用的Wrapper。

首先将this.createURL提取出来,作为一个通用的接口,由于createURL是一个函数,所以接口形式如下:

@FunctionalInterface

public interface RuntimeWrappableFunction {

R apply(T t) throws Exception;

}

接下来,由于map的参数是一个函数式接口,所以我们来完成一个消费上述函数接口的实现:

public class RuntimeWrappableFunctionMapper {

public static Function wrap(RuntimeWrappableFunction wrappableFunction) {

return t -> {

try {

return wrappableFunction.apply(t);

} catch (Exception e) {

throw new RuntimeException();

}

};

}

}

完善原有的代码:

public class UrlHandler {

public List getURLs() {

return Stream.of("http://www.baidu.com", "https://www.google.com")

.map(RuntimeWrappableFunctionMapper.wrap(this::createURL))

.collect(Collectors.toList());

}

private URL createURL(String url) throws MalformedURLException {

return new URL(url);

}

}

有一些可选的库也可以完成上述的功能。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/213779
推荐阅读
相关标签
  

闽ICP备14008679号