Reviewing catch blocks
Here’s an interesting exercise. If you’re writing code in a language like C# or C++ that has catch statements, write a script to report all catch blocks. You might be surprised at what you find. Some questions to ask:
- Do catch blocks swallow exceptions and thus mask problems?
- Is information lost by catching an exception and throwing a new one?
- Are exceptions logged appropriately?
- Are notification messages grammatically correct and helpful?
Here’s a PowerShell script that will report all catch statements plus the five lines following the catch statement.
Related post: Finding embarrassing and unhelpful error messages