Does wrapping everything in try/catch blocks constitute defensive programming?

My basic rule is : Unless you can fix the problem which caused the exception, do not catch it, let it bubble up to a level where it can be dealt with.

In my experience, 95% of all catch blocks either just ignore the exception (catch {}) or merely log the error and rethrow the exception. The latter might seem like the right thing to do, but in practice, when this is done at every level, you merely end up with your log cluttered with five copies of the same error message. Usually these apps have an “ignore catch” at the top most level (since “we have try/catch at all the lower levels”), resulting in a very slow app with lots of missed exceptions, and an error log that too long for anyone to be willing to look through it.

Leave a Comment