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:
and that
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)
Which Exception should I release according to each situation?, Why should we avoid returning error codes? and Exceptions should be used for flow control?. There’s more to the exception or Exception tag
– rray
What does ELI5 mean?
– user28595
means explaining in a way that even a 5 year old child would understand (I do not know if the subject allows)
– wdarking
Too much of a sincere answer: use when you have absolutely no other way to solve the problem (which is rare).
– Bacco
short and thick answer. liked. if put this in an answer and an example the question is yours, bacco ;)
– wdarking
@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.
– Bacco
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.
– Bacco