[hyddd的Fortify SCA分析Java代码记录][Structural]Poor Error Handing:Overly Broad Catch
2009-02-26 11:53 hyddd 阅读(3795) 评论(0) 编辑 收藏 举报这个问题和[M D REC] Exception is caught when Exception is not thrown里面说的第二种情况相同,示例代码如下
try{
//IOoperation
//
}
catch(Exception ex){
Log(ex);
}
Fortify建议你分别处理可能出现的异常,因为不同类型的异常需要不同的处理方法,所以应该把try{}里可能出现的异常都枚举出来,然后分别处理,正确的代码写法如下:
//IOoperation
//
}
catch(Exception ex){
Log(ex);
}
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);
}
//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);
}
作者:hyddd
出处:http://www.cnblogs.com/hyddd/
本文版权归作者所有,欢迎转载,演绎或用于商业目的,但是必须说明本文出处(包含链接)。