正则表达式替换

在项目里面看到好多jsp或java类都是只捕获异常,在异常处理中却是空的,什么都没写,如果程序出现异常,没有一点可用的线索,例如

 

try{			
		String strSql="";
		conn=DataSourceBean.getConnection();
		ps=conn.prepareStatement(strSql);
		rs=ps.executeQuery();
		
}catch(Exception ex){    }
finally{
		DatasourceHelper.executeClose(rs,ps,null,conn);
		rs=null;
		ps=null;
}

 

今天帮同事解决一个问题,正好用到了正则表达式批量替换,顺便也解决一下项目中的异常处理空的问题。

 

解决方法:在ultraEdit中批量替换所有存在此类问题的文件。

 

用于查找
(catch)\s*(\(\w*Exception\s+)(\w+)\)\{

用于替换
\1\2\3){\3.printStackTrace();
 

 


正则表达式替换

 

经过替换后的代码:

 

try{			
		String strSql="";
		conn=DataSourceBean.getConnection();
		ps=conn.prepareStatement(strSql);
		rs=ps.executeQuery();
		
}catch(Exception ex){ex.printStackTrace();    }
finally{
		DatasourceHelper.executeClose(rs,ps,null,conn);
		rs=null;
		ps=null;
}