Skip to content

CkNumberException

What CkNumber throws — only when asked.

Raised by any CkNumber method called with throwIfInvalid: true when a value cannot be converted. Never thrown otherwise: by default the readers answer the caller's fallback and the validators answer false instead. The OrNull twins and Explain cannot raise it at all: they take no throwIfInvalid and never throw — null is the twins' answer for any error. Final, and extends CkException.

The message names the method, shows the value with its type, names the type it was meant to become and says why it failed:

CkNumber.TinyInt: cannot convert 300 (Int32) to Byte — outside 0 to 255.

Properties

Property Type What it holds
TargetType string The numeric type the value was to become, by the name the library's messages use — 'Int64' for an int, 'Double' for a float, 'Byte' for an unsigned TINYINT, 'Decimal' for Decimal, Round, Divide and Percent.
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->TargetType, $error->Input, $error->Reason.

Quick look

use SafeCoder\CommonKit\CkNumber;
use SafeCoder\CommonKit\Exceptions\CkNumberException;

try {
    CkNumber::TinyInt(
        input: 300,
        throwIfInvalid: true
    );
} catch (CkNumberException $error) {
    echo $error->Reason;        // -> 'outside 0 to 255'
    echo $error->TargetType;    // -> 'Byte'
}