Monday, April 8, 2013

RegEx to find empty/swallowed exception blocks with Visual Studio 2012



RegEx:
catch\s\(.*Exception.*\)*\s*(\n?)*\{\s*(//.*)*\s*\n?\}

It will match following formats

catch (Exception)
{
}

catch (Exception){}

catch (Exception)
{
//any c# comments
}

catch (Exception){
}

catch (Exception){
 //any c# comments
}

catch (Exception ex)
{
}
catch (Exception ex){
//any c# comments
}


 For details on searching in visual studio 2012 using regex:
http://msdn.microsoft.com/en-us/library/vstudio/2k3te2cs.aspx