Everything I've read says that they should be used when something that simply "shouldn't happen" does happen. But the thing is, there's a lot of things that could go wrong.
For example, if I'm accessing an object's property via the magic __get($property)
method, it's possible that there would be no property called $property
. So does this mean that I should wrap every $foo->bar
in a try/catch?
And what about database queries? Do I really need to wrap every $stmt->execute()
in a try/catch statement?
I feel like if I wrapped everything that could go wrong in a try/catch statement, I'd end up with several dozen try/catches per file!
catch
blocks? If you're going to do something useful, like display a friendly dialog saying that a file wasn't found, then it's worthwhile. If you're just going to mimic the same "can't access nonexistent property" error message that would happen without the try/catch, there's literally no point.