Handling complex results in objects - exceptions?

What is the general way to handle complex results from functions with an object? A simple method can return true or false, and that’s all I need to know. A more complex method may return true or false, but there may be more information that I would like to get out of the class, for example, why this failed (there may be several reasons).

I could do this with an exception, but I read in some places that using an exception for "normal" functions is not good practice. Also, since throwing an exception destroys the object, I cannot do anything with it, so the exception will not work if I want to continue using this object. It seems to me that an exception should be thrown when something is really wrong, and not an expected, recoverable error.

So, another way is to get the result property after the method starts, and if this method returns false, I can ask what its state is. Is this a way to do this?

+3
source share
3 answers

: , ("success" => false, "explanation" => "saw a mouse and ran screaming like a little girl", "solution" => "call an exterminator").

+1

, , , - .

, , , .

- . .

, . - , , .

, , .

0

I suggest you always support predictable method signatures, i.e. always return the same type (be it an array, a boolean, something else.)

I think the example of checking with the helpZend_Validate uses a reasonable solution to your problem, differentiating the result from a possible error.

0
source

Source: https://habr.com/ru/post/1727673/


All Articles