java checkedexception_如何处理Java 中的Checked Exception

想必很多人对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);

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值