When I use the Exception provided by the SPL the options are limited. So it's hard to pick which exception would be best suited to handle File exceptions.

I know the best scenario is to write my own Exception (FileException or something), but I was wondering if the SPL could provide of some use here.

I'm currently thinking that the RuntimeException might be the best candidate, because the documentation states:

Exception thrown if an error which can only be found on runtime occurs.

And for files this is mostly the case. For example:

  • File does not exist
  • File is not readable
  • File cannot be opened

What is the PHP community's thought on this one?

1 Answer 1


My suggestion is to have only RuntimeException. When there are a lot of exception types -- let's face it, you're using them for flow control, which is always evil.

This correlates to fail fast approach, which is reflected in a great Antifragile book. In essence, it means that you should expose your problems immediately and not to try to hide them. Stop code execution right where the problem happened. Log it. Be nice to your users -- say sorry. And don't propagate the error further, usually nothing good happens in this case.

This approach usually is opposed to defensive programming, but it depends how to interpret this term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.