CkMoneyException
What CkMoney throws — only when asked.
Raised by any CkMoney method called with
throwIfInvalid: true when a value could not be used. Never thrown otherwise: by
default every method answers the caller's fallback, '', false, an empty array or a
zero Money instead — and AmountOrNull, ToMoneyOrNull, InCurrencies and Explain
cannot raise it at all: they take no throwIfInvalid and never throw, null or false
being their answer for any error. Final, and extends CkException.
The message names the method, shows the value with its type, says what it was meant to be and why it failed:
CkMoney.MinorUnits: cannot use "N/A" (String) as an amount of money — letters in the amount.
Properties
| Property | Type | What it holds |
|---|---|---|
Expected |
string |
What the value was meant to be — 'an amount of money', 'a currency code', 'a factor'. |
Input |
mixed |
From CkException: the value that failed, untouched. |
Reason |
string |
From CkException: why, in a few words. |
All three are public and read-only: $error->Expected, $error->Input, $error->Reason.
Quick look
use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\Exceptions\CkMoneyException;
try {
CkMoney::Amount(
input: 'N/A',
throwIfInvalid: true
);
} catch (CkMoneyException $error) {
echo $error->Reason; // -> 'letters in the amount'
echo $error->Expected; // -> 'an amount of money'
}