2009-03-02

The art of exceptions catching

Exceptions in programming languages are very useful tool for propagation information about program errors and helping handling critical situations properly. But it is only tool and how it can be useful depends only on implementing programmer.

I can put it in few points what I have learned in theory and verified during practice:
  • If you don't know what to do with exceptions don't catch'em - exceptions reveal errors so fix errors - don't hide them under carpet
  • Catch exception if you can handle it, you know how to clean up used resources and how to inform developers/users about that event
  • Don't use exceptions mechanism (hacky way) in the correct flow of the program
  • In Java use unchecked exceptions (RuntimeExceptions descendants) instead of checked Exceptions - it saves "throws SomeException" litter in code forcefully reminding about catching them (it makes sense only in some kind of low level services with inevitable critical situations that can be handled by direct clients)

I think it's only basic set of rules.

Do you have another ideas for exception handling?

No comments: