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
causeis 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()
WhenResult.try() is called without a custom catch handler:
Result.tryPromise()
WhenResult.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
-
Prefer custom catch handlers for better error types:
-
Use for quick prototyping, refine later:
-
Log the cause for debugging:
-
Check cause type when handling:
See Also
- Result.try - Catching synchronous exceptions
- Result.tryPromise - Catching async exceptions
- Panic - Unrecoverable errors (defects)
- TaggedError - Base class for UnhandledException