1. findbugs官方解释:
| Exception is caught when Exception is not thrown This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs. A better approach is to either explicitly catch the specific exceptions that are thrown, or to explicitly catch RuntimeException exception, rethrow it, and then catch all non-Runtime Exceptions, as shown below: try { ... } catch (RuntimeException e) { throw e; } catch (Exception e) { ... deal with all non-runtime exceptions ... } |
2. 一般人都会这样写代码:
try{
//
}
catch(Exception ex){
//
}
try{
//
}
catch(SQLException ex){
//
}
catch(IOException ex){
//
}
catch(Exception ex){
//
}
本文探讨了Java中异常处理的常见误区与最佳实践,强调了避免过泛地捕获异常的重要性,并提供了具体的代码示例说明如何正确处理不同类型的异常。
316

被折叠的 条评论
为什么被折叠?



