Hunting down bad try..catch blocks
Way too often developers take the easy solution to use try..catch blocks to silence and ignore exceptions.
There are two things to do when you see this happening:
- Explain the developers why it's a bad practice (exceptions are costly and their use should be reserved to exceptional cases), and teach them how to do better (use TryParse, test upfront for known problematic values, etc.)
- Hunt down the bad blocks and remove them whenever possible
To spot try..catch blocks with empty catch statements, you can use something simple: Visual Studio's search feature, which allows you to search in files. Of course, you can start by looking for catch{} and catch {} and catch { } and catch {}, but that makes several cases, and doesn't cover the cases where there are line breaks. Luckily, Visual Studio's search support regular expressions, so you can devise one that would match all cases. To get you started, here are some you can use catch.*:b*\n:b*\{:b*\n*:b*\} and catch.*:b*\n:b*\{\} and catch.*:b*\n:b*\{.*\}
I wrote these quickly, so they can be improved. Any expert in regular expression should be able to write a single expression that matches all cases... This could be further improved to include the catch blocks that contain comments such as // ignore exceptions or /* simply ignore this problem */.