When to use Exceptions in PHP?

Asked

Viewed 302 times

3

I arrived in a module of the OOP PHP workbook on Handling Exceptions, I practiced the exercises and such, but I’m still not quite sure when I should use them, and why. I did a search and found this:

https://stackoverflow.com/questions/935490/why-and-how-would-you-use-exceptions-in-this-sample-php-code

and that

http://php.net/manual/en/language.exceptions.php

the S.O question seemed more enlightening, as the php doc talks more about 'how to use' the Exceptions. The answer that was accepted by @Tower is explanatory, but in the question it describes some code situations where he wanted to know whether or not to use exceptions, but in the answer he does not use any of the code used in the question. There is also @rball’s answer, and there he gives a different notion of exceptions. I would like to know from you, when should I use them, and if possible, explain each situation proposed by the aforementioned question.

ps.: I know there is an identical question, but the answer was translated from the question I quoted above , and as I said, the answer @Tower accepted does not describe the use of Exceptions in the codes he gave as an example.

ps2.: ELI5 (Explain Like I’m Five / Explain to me as if I were a 5-year-old child)

  • 2
  • What does ELI5 mean?

  • means explaining in a way that even a 5 year old child would understand (I do not know if the subject allows)

  • 2

    Too much of a sincere answer: use when you have absolutely no other way to solve the problem (which is rare).

  • 1

    short and thick answer. liked. if put this in an answer and an example the question is yours, bacco ;)

  • 4

    @wdarking I don’t think it would look cool because it wouldn’t have Exception :) - It’s hard to talk about something that is often used in PHP simply because the person came from another language and wants to use the same culture. And unfortunately, in PHP this happens too much. Exceptions, MVC, OOP, and other concepts that almost always result in disasters in the practical use of day-to-day, when people apply in PHP because someone said it was good. Some people use "right" this in PHP, but unfortunately are rare exceptions. And the irony of these cases is that it’s usually the ones who use only the bare minimum.

  • 3

    PS: Anyway, I’m curious to read answers that give legitimate example(s) of use of exceptions. You can still get some good answers, if someone who understands well the "soul" of PHP elaborate a post.

Show 2 more comments

1 answer

2


I usually use whenever a data input error happens, or when something is expected to exist and this something does not exist, whenever your logic can be broken, then you make an exception.

More important than using exceptions, I think it is knowing how to position the Try-catch block, which can be used with different catch levels and also a Try-catch block within another through the encapsulation of layers that a structural Pattern can generate.

One advantage of using Try-catch blocks in different layers is the error control that is most effective, avoiding breaking into higher application layers (Controllers), avoiding fatal errors or 500.

Some frameworks implement handlers of errors and levels of different exceptions, all extend to the same object Exception and are captured at different levels as well.

If you don’t capture, the framework usually tries to identify the type of Exception and their code to also know how to respond to the request to the user.

Some types of exception generate errors 500, 404, 401, among others.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.