代码改变世界

[hyddd的Fortify SCA分析Java代码记录][Structural]Poor Error Handing:Overly Broad Catch

2009-02-26 11:53  hyddd  阅读(3775)  评论(0编辑  收藏  举报

这个问题和[M D REC] Exception is caught when Exception is not thrown里面说的第二种情况相同,示例代码如下

try{
    
//IOoperation
    
//
}
catch(Exception ex){
    Log(ex);
}
Fortify建议你分别处理可能出现的异常,因为不同类型的异常需要不同的处理方法,所以应该把try{}里可能出现的异常都枚举出来,然后分别处理,正确的代码写法如下:

  try {
    
//IOoperation
    
//
  }
  
catch (IOException e) {
    logger.error(
"doExchange failed", e);
  }
  
catch (InvocationTargetException e) {
    logger.error(
"doExchange failed", e);
  }
  
catch (SQLException e) {
    logger.error(
"doExchange failed", e);
  }