正则表达式查找未记录的异常

在旧代码中,有一些地方只是写了catch{} ,但没有把异常信息记录下来,导致了分析查找问题的原因过久,但手动去查找哪儿没有捕获异常,所需要花费的时间又太长,以前有写过一次,但后来丢了,现在又要用到,先蹩脚地记录下来,给自己用的

情景一:
catch (Exception ex)
{

}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]*(Exception ex) [ ]+{ [ ]+ [ ]+}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }


情景二:
catch (Exception ex)
{
}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]*(Exception ex) [ ]+{ [ ]+}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }


情景三:
catch( )
{
}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]*([ ]*) [ ]+{ [ ]+}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }


情景四:
catch( )
{}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]*([ ]*) [ ]+{[ ]*}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }

情景五:
catch
{
}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]* [ ]+{[ ]*}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }

情景六:
catch
{

}
=>
catch (Exception ex)
{
TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex);
}
查找内容: [ ]+catch[ ]* [ ]+{ [ ]*}
替换对象: catch (Exception ex) { TxtLog.WriteException(string.Format("任务处理错误,错误信息:{0}", ex.Message), ex); }

关于异常处理方法,看另外一篇博客

http://www.cnblogs.com/maanshancss/p/4691197.html

原文地址:https://www.cnblogs.com/maanshancss/p/4647525.html