Да, наследуя Exception или RuntimeException/LogicException:
class InsufficientFundsException extends RuntimeException {
public function __construct(
public readonly float $balance,
public readonly float $amount,
) {
parent::__construct(
sprintf('Insufficient funds: balance %.2f, requested %.2f', $balance, $amount)
);
}
}
try {
throw new InsufficientFundsException(100.0, 150.0);
} catch (InsufficientFundsException $e) {
echo "Balance: {$e->balance}, needed: {$e->amount}";
}Рекомендации: наследуйте RuntimeException для ошибок в runtime, LogicException для ошибок в логике кода.