Skip to main content

Overview

UnhandledException wraps exceptions that are caught by Result.try() or Result.tryPromise() when no custom catch handler is provided. It automatically derives a descriptive error message from the caught exception. This error type represents expected exceptions that are handled gracefully by the Result abstraction, as opposed to Panic which represents unrecoverable defects.

Class Definition

Properties

'UnhandledException'
required
Discriminator tag, always "UnhandledException".
string
required
Automatically derived message:
  • If cause is an Error: "Unhandled exception: <error.message>"
  • Otherwise: "Unhandled exception: <String(cause)>"
unknown
required
The original exception that was caught. Can be any value (Error, string, object, etc.).
string
Error name, set to "UnhandledException".
string | undefined
Stack trace. If cause is an Error, includes “Caused by:” chain with the original stack.

Constructor

unknown
required
The exception that was caught. Can be any value.

Examples

When It’s Created

Result.try()

When Result.try() is called without a custom catch handler:

Result.tryPromise()

When Result.tryPromise() is called without a custom catch handler:

Usage Patterns

Basic Error Handling

Type Guard

Check if an error is an UnhandledException:

Accessing the Original Exception

Pattern Matching

Avoiding UnhandledException

Use custom catch handlers to convert exceptions into domain-specific errors:

With Result.try()

With Result.tryPromise()

Comparison with Panic

Error Message Format

The message is automatically formatted based on the cause type:

Stack Trace Chaining

When the cause is an Error, stack traces are chained:

JSON Serialization

Best Practices

  1. Prefer custom catch handlers for better error types:
  2. Use for quick prototyping, refine later:
  3. Log the cause for debugging:
  4. Check cause type when handling:

See Also