PHP - Exceptions!

As we transition to (re)build a major website from almost the ground up, we’re going to be moving towards exceptions + exception handling.

Honestly I’m pretty new to the flow/process but I wonder how this generally works for people? Has it been a positive move?

How granular do your exceptions get?

Do you have global container exceptions, or do you have class-level exceptions (or both?)

if you recommend any good reads, please pass it along. Hoping to get some insight and learn as much as possible!

I usually surround try-catch blocks around anything that has to go externally from PHP, such as file requests, establishing database connections, cURL/fopen requests, etc

Good advice.

One thing I always taught my Java students (and it applies here) is to make sure your exception handling is a tight (localized) as possible.

In other words only surround the code that could throw an exception, I see way too many people surround the entire script in the try catch when in fact there’s only a few lines of code where it could happen. It not only makes them easier to manage it also makes handling exceptions a lot more efficient.

Very good straightforward advice… good way to look at it :slight_smile:

Thanks

Do you have things like CURLConnectionException, FileNotFoundException, FileReadException, FileWriteException, etc… or do you go more general?