Lazycoder

17Aug/040

re-throwing exceptions

Exception Handling – Too Much ‘Faith’

From this post by Lamont, David Truxall comments,

Re-throwing tends to bury the real source of the error when you are trying to debug a problem in a multi-tier application.

It does bury it, but it should be burried in the innerException property of the Exception you throw. I’m not a big believe in a lot of things when it comes to exception handling but I do strongly believe one thing. If you are going to catch an Exception and throw a custom one, you should always, ALWAYS pass in the caught Excetption as the second argument of your new Exception. Let the calling method see the original Excepton if it wants to.

I think if the Exception can be handled by your method, it should be handled by your method (For example, calling a “cleanCache” method if you receive an IOException, “Hard drive full” Exception and your application stores temp files in a cache.) But understand what can be handled and should be caught and what can’t be handled and should just be reported.

Filed under: General Leave a comment