Skip to content

CkDateException

What CkDate throws — only when asked.

Raised by any CkDate method called with throwIfInvalid: true when neither the value nor the fallback can be used. Never thrown otherwise: by default those methods answer their fallback — or the year-one sentinel — 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, says what it was meant to be and why it failed:

CkDate.ToDateOnly: cannot use "31/12/2026" (String) as a date — not a date in month-first order.

Properties

Property Type What it holds
Expected string What the value was meant to be — "a date", "an as-of date", "a birth date", "the other date", "an output format". A zone or assumeZone id the library does not know is never the reason: it counts as UTC.
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
{
    // day 31 cannot be a month, so month-first reading fails
    CkDate.ToDateOnly(
        input: "31/12/2026",
        throwIfInvalid: true
    );
}
catch (CkDateException error)
{
    Console.WriteLine(error.Reason);    // -> "not a date in month-first order"
    Console.WriteLine(error.Expected);  // -> "a date"
}