When to use Try Catch blocks

It seems to me that this topic is very strange and confused. Could someone lights me up?

Definitely. I’m not a PHP user, but I might have a little insight after having worked with try/catch in ActionScript, Java, and JavaScript. Bear in mind though, that different languages and platforms encourage different uses for try/catch. That said…

The only times I’d recommend using try/catch is if you’re using a native language function that

  1. Can throw an error/exception
  2. Does not give you any tools to detect whether you’re about to do something stupid that would cause that error/exception. eg: In ActionScript, closing a loader that is not open will result in an error but the loader doesn’t have an isOpen property to check so you’re forced to wrap it in try/catch to silence an otherwise totally meaningless error.
  3. The error/exception really is meaningless.

Let’s take the examples you list and see how they square with that list.

I read someone saying that we should use try-catch blocks only to prevent fatal errors.

In the case of AS’s loader.close() function, this is good advice. That’s a fatal error, and all from an otherwise trivial misstep. On the other hand, virtually ALL errors in AS will bring your application to a halt. Would you then wrap them all in try/catch? Absolutely not! A “fatal error” is fatal for a reason. It means something terribly wrong has happened and for the application to continue on in a potentially “undefined” state is foolhardy. It’s better to know an error happened and then fix it rather than just let it go.

I read someone else saying that we should use it only on unexpected errors

That’s even worse. Those are presicely the errors you DON’T want to silence, because silencing them means that you’re never going to find them. Maybe you’re not swallowing them, though… maybe you’re logging them. But why would you try/catch/log/continue as though nothing happened, allowing the program to run in a potentially dangerous and unexpected condition? Just let the error kick you in the teeth and then fix it. There’s little more frustrating than trying to debug something that’s wrong in a program that someone else wrote because they wrapped everything in a try/catch block and then neglected to log.

Others simply say that try-catch blocks should be used everywhere because they can be also extended (extending the Exception class).

There’s potential merit to this if you’re the one doing the throwing, and you’re trying to alert yourself to an exceptional situation in your program… but why try/catch your own thrown error? Let it kick you in the teeth, then fix it so that you don’t need to throw the error anymore.

Finally someone says that PHP try-catch block are totally useless because they are very bad implemented. (On this i find a nice SO question about performance).

Maybe so. I can’t answer this one though.

So… this might be a bit of a religious question, and I’m certain people will disagree with me, but from my particular vantage point those are the lessons I’ve learned over the years about try/catch.

Leave a Comment