What is the correct way to use exceptions with PHP?

I always have a "return status code" method for managing errors. Exceptions now seem to be the best way to manage errors.
But is this really the best way? I read things like exceptions worse than GoTo

Do you know good articles / posts / books about exceptions and error management?

+4
source share
2 answers

If you are programming a oriented object, perhaps exceptions are better for you. If you write procedural, return values ​​are the best way. It depends on your coding style, etc.

+3
source

Exceptions are approximately 10,000x slower than returning a status / error code depending on the programming language. This is because all stack data is tracked. This is bad.

In general, you will never have to use exceptions. In fact, at some point, the only thing that existed was the return codes.

The good thing about exceptions is that they will not allow the program to continue working if they are not processed correctly. Instead, the program crashes.

So, basically, if you forget about the processing of status messages or worry that others might not check return codes from your functions, exceptions stop the program and force it to fix. Although, I saw how many inexperienced programmers catch exceptions, do nothing with it, and then continue. This is basically the same as ignoring the return code from a function.

Another nice thing about exceptions is that they β€œbubble” automatically. Instead of passing error codes through a long chain of functions, you can set up your try catch at the highest level and handle any error accordingly (assuming you don't want anything else going on between them). For example, if something goes wrong, display the error page.

+2
source

All Articles