// Базовое исключение приложения
class AppException extends RuntimeException {}
// Специализированные исключения
class NotFoundException extends AppException {
public static function forEntity(string $entity, mixed $id): self {
return new self("$entity with ID $id not found");
}
}
class ValidationException extends AppException {
public function __construct(
private array $errors,
string $message = 'Validation failed',
) {
parent::__construct($message);
}
public function getErrors(): array { return $this->errors; }
}
// Использование
throw NotFoundException::forEntity('User', 42);Создавайте иерархию исключений для разных типов ошибок - это позволяет ловить их группами.