sergey_web3 Nov 2025 12:42

In PHP the standard is to throw typed exceptions. Some teams use error codes (return value or error object) for domain errors. Where do you draw the line?

Replies (6)
alex_petrov3 Nov 2025 13:04

Exceptions for exceptional conditions: database is down, required resource not found, precondition violated. Expected domain outcomes (user not found in a lookup) should be a return value: nullable, Option type, or Result type.

0
nphp3 Nov 2025 13:45

The test for “use exception vs return value”: would a caller realistically want to continue execution if this happens without catching? If yes, return a value. If the only sane response is to bail, throw.

0
ivan_morozov3 Nov 2025 15:35

In API controllers, validation failures are better as return values because the controller needs to format the error response. Using exceptions for validation (Laravel does this with HttpException) works but blurs the control flow.

0
vova3 Nov 2025 16:34

Over-using exceptions as flow control is a PHP antipattern. Throwing and catching is expensive compared to returning a value. For loops or repeated operations, exceptions in the hot path matter.

0
sergey_web3 Nov 2025 16:39

Typed exception hierarchies (UserNotFoundException, PermissionDeniedException, etc.) are useful for API exception handlers that translate domain exceptions to HTTP responses. One handler per exception type, no type-checking switch.

0
katedev3 Nov 2025 17:21

We have a ValidationException that carries field-level errors. It is thrown by validation service classes and caught by a global exception handler that formats the 422 response. Exception as control flow, but at a well-defined layer boundary.

0
Write a reply
Markdown. ```php blocks are runnable.