Skip to content

CkException

The base of every exception CommonKit throws.

CommonKit throws only when you ask it to — with throwIfInvalid: true. By default every method answers its rejection value instead, and no exception exists. The OrNull readers go further still: they take no throwIfInvalid at all and never throw — null is their answer for any error. When you do ask, each class raises its own type — CkTextException, CkNumberException, and so on — and every one of them derives from this abstract base. That gives you a choice: catch the specific type to handle one kind of failure, or catch CkException to handle them all alike.

Properties

Property Type What it holds
Input object? The value that could not be handled, exactly as it was passed in.
Reason string Why it could not, in a few words — "not a number", "outside 0 to 255".

The derived types add their own facts on top: most carry Expected (what the value was meant to be), CkNumberException carries TargetType, and CkDictionaryException carries the Key that failed. Each type's page lists its extras.

The message is one safe log line

Every message is composed the same way — the method, the value with its runtime type, and the reason:

CkCountry.Alpha2: cannot use "Alasca" (String) as a country — not a recognised country or code.

Two things are done on purpose:

  • Control characters become spaces. A hostile value full of line breaks or terminal escapes cannot write its own log lines or color your console — the message stays one honest line. The Input property keeps the raw value untouched.
  • Long values are cut. Text is quoted and truncated to a readable length, so a ten-megabyte payload does not become a ten-megabyte log entry.

Quick look

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

try
{
    CkNumber.Int32(
        input: "banana",
        throwIfInvalid: true
    );
}
catch (CkException error)               // catches every CommonKit exception
{
    Console.WriteLine(error.Reason);    // a few plain words
    Console.WriteLine(error.Input);     // the exact value that failed
}

The types

One per class: CkBoolException, CkCountryException, CkDateException, CkDateTimeException, CkDictionaryException, CkEmailException, CkFileException, CkGeoException, CkIntlException, CkIPException, CkMoneyException, CkNumberException, CkPayloadException, CkTextException, CkUSException, CkUuidException, CkWebException.