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 those methods answer the caller's fallback instead. The OrNull twins cannot raise it at all: they take no throwIfInvalid and never throw — null is their answer for any error. Sealed, and derived from 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.Byte: cannot convert 300 (Int32) to Byte — outside 0 to 255.

Properties

Property Type What it holds
TargetType Type The numeric type the value was to become.
Input object? From CkException: the value that failed, untouched.
Reason string From CkException: why, in a few words.

Quick look

using SafeCoder.CommonKit;
using SafeCoder.CommonKit.Exceptions;

try
{
    CkNumber.Byte(
        input: 300,
        throwIfInvalid: true
    );
}
catch (CkNumberException error)
{
    Console.WriteLine(error.Reason);           // -> "outside 0 to 255"
    Console.WriteLine(error.TargetType.Name);  // -> "Byte"
}