Is catching general exceptions really a bad thing?
Posted
by
Bob Horn
on Programmers
See other posts from Programmers
or by Bob Horn
Published on 2012-09-09T15:18:57Z
Indexed on
2012/09/09
15:48 UTC
Read the original article
Hit count: 339
I typically agree with most code analysis warnings, and I try to adhere to them. However, I'm having a harder time with this one:
CA1031: Do not catch general exception types
I understand the rationale for this rule. But, in practice, if I want to take the same action regardless of the exception thrown, why would I handle each one specifically? Furthermore, if I handle specific exceptions, what if the code I'm calling changes to throw a new exception in the future? Now I have to change my code to handle that new exception. Whereas if I simply caught Exception
my code doesn't have to change.
For example, if Foo calls Bar, and Foo needs to stop processing regardless of the type of exception thrown by Bar, is there any advantage in being specific about the type of exception I'm catching?
© Programmers or respective owner