Skip to content

try

Advanced exception handling with multiple handlers. Feather's try implementation is fully compatible with TCL 8.6+.

Syntax

tcl
try body ?handler...? ?finally script?

Where handlers can be:

  • on code varList script - matches specific return codes
  • trap pattern varList script - matches error codes by prefix

Parameters

  • body: The script to execute
  • on code varList script: Handle specific return code
  • trap pattern varList script: Handle errors matching errorcode pattern
  • finally script: Script that always executes (regardless of body/handler outcome)

Return Codes

The on handler matches these completion codes:

CodeNameMeaning
0okNormal completion
1errorError occurred
2returnreturn command
3breakbreak command
4continuecontinue command

Integer code values are also supported.

Trap Pattern Matching

The trap handler matches errors by -errorcode prefix:

  • Empty pattern {} matches all errors
  • Pattern {A B} matches errorcode {A B} or {A B C} (prefix matching)
  • First matching handler is selected; on error will mask subsequent trap handlers

Handler Variables

The varList can be:

  • Empty: no variables bound
  • One variable: receives the result or error message
  • Two variables: receives result and options dictionary

Use - as script to fall through to next handler. The first matching handler's varList is used for variable binding.

Finally Clause

  • Always executed regardless of body/handler outcome
  • If finally raises an error, it overrides previous result
  • Otherwise, preserves the result from body or handler

Exception Context

If an exception occurs during a handler or finally clause, the original exception's status dictionary is added to the new exception under the -during key. This preserves context about what exception was being handled when the new error occurred.

Examples

Output
Output

See Also