Skip to content

CkBoolException

What CkBool throws — only when asked.

Raised by any CkBool method called with throwIfInvalid: true when a value could not be read as a boolean (a true-or-false value). Never thrown otherwise: by default the readers answer your fallback, Format answers "" and the validators answer false instead — and the OrNull twins and Explain cannot raise it at all: they take no throwIfInvalid and never throw. A value that is readable never throws, whatever it says. 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:

CkBool.FromValue: cannot use "maybe" (String) as a boolean — not a recognised true/false value.

CkDictionary.Bool and CkText.BoolToString read through CkBool but keep raising their own exceptions — CkDictionaryException and CkTextException — carrying the same Reason.

Properties

Property Type What it holds
Expected string What the value was meant to be — "a boolean", "a 1 or a 0", "a true/false word".
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
{
    CkBool.FromNumber(
        input: 2,
        throwIfInvalid: true
    );
}
catch (CkBoolException error)
{
    Console.WriteLine(error.Reason);    // -> "a number that is not 1 or 0"
    Console.WriteLine(error.Expected);  // -> "a 1 or a 0"
}