多catch块折叠

多catch块的代码优化

通常情况下编码的时候,针对不同的异常类型,会使用多个catch块包住,来对各自的异常做处理,如下所示:

try{
...
}catch(***Exception e){
    e.printStackTrace();
}catch(***Exception e){
    e.printStackTrace();
}

通常会提示如下警告:

Reports identical catch sections in try blocks under JDK 7. A quickfix is available to collapse the sections into a multi-catch section.
This inspection only reports if the project or module is configured to use a language level of 7.0 or higher.

我这个人有强迫症,看到警告就想去掉,所以就了解了一下这个警告的意思,大致就是说,jdk7之后把catch块代码折叠起来更高效。

try{
...
}catch(***Exception | ***Exception e){
    e.printStackTrace();
}
posted @ 2020-01-14 17:25  itwetouch  阅读(317)  评论(0编辑  收藏  举报