try(3tcl)
π try β bestie, we're catching exceptions π
okay so this is try(3tcl), a page from the tcllib manual, and it's the forward compatibility implementation of [try]. pull up a chair, we're doing error handling and it's gonna be gorgeous π
β¨ NAME β¨
try β trap and process errors and exceptions. like, catch your chaos before it catches you π«Ά
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require try ?1.1?
::try body ?handler...? ?finally script?
β¨ DESCRIPTION β¨
okay bestie, here's the tea β this package gives you a forward-compatibility implementation of Tcl 8.6's try/finally command (that's TIP 329, iykyk) but for Tcl 8.5. and the code? it was directly pulled from Tcl 8.6 revision ?, back when try/finally was implemented as a Tcl procedure instead of in C. no cap.
π¦ ::try body ?handler...? ?finally script?
this command runs the script body and then, depending on what the outcome of that script is β normal exit, error, or some other exceptional result β it runs a handler script to deal with the case. main character energy fr fr π
once all that's happened, if the finally clause is present, the script it includes will be run, and the result of the handler (or the body if no handler matched) is allowed to continue to propagate. and listen β the finally clause gets processed even if an error occurs and irrespective of which, if any, handler is used. it's loyal like that π
the handler clauses are each expressed as several words, and they must have one of the following forms:
πΈ on code variableList script
this clause matches if the evaluation of body completed with the exception code code. the code may be expressed as an integer or one of the following literal words: ok, error, return, break, or continue. those literals correspond to the integers 0 through 4 respectively. cute, right?
πΈ trap pattern variableList script
this clause matches if the evaluation of body resulted in an error and the prefix of the -errorcode from the interpreter's status dictionary is equal to the pattern. the number of prefix words taken from the -errorcode is equal to the list-length of pattern, and inter-word spaces are normalized in both the -errorcode and pattern before comparison. so it's a whole vibe-check match, basically β
okay now the details that apply to every handler, pay attention bestie:
- the
variableListword in each handler is always interpreted as a list of variable names.- if the first word of the list is present and non-empty, it names a variable into which the result of the evaluation of
body(from the maintry) will be placed. this will contain the human-readable form of any errors. π - if the second word of the list is present and non-empty, it names a variable into which the options dictionary of the interpreter at the moment of completion of execution of
bodywill be placed.
- if the first word of the list is present and non-empty, it names a variable into which the result of the evaluation of
- the
scriptword of each handler is also always interpreted the same way: as a Tcl script to evaluate if the clause is matched.- if
scriptis a literal-and the handler is not the last one, the script of the following handler is invoked instead β just like with theswitchcommand. iconic behavior tbh.
- if
- note that handler clauses are matched against in order, and the first matching one is always selected. at most one handler clause will be selected. as a consequence, an
on errorwill mask any subsequenttrapin thetry. π also note thaton erroris equivalent totrap {}. periodt.
β οΈ okay this one matters: if an exception (i.e. any non-okresult) occurs during the evaluation of either the handler or thefinallyclause, the original exception's status dictionary will be added to the new exception's status dictionary under the-duringkey. so nothing gets lost, it just gets nested. π
β¨ EXAMPLES β¨
π example one: make sure a file gets closed no matter what
set f [open /some/file/name a]
try {
puts \$f "some message"
# ...
} finally {
close \$f
}
π example two: handle different reasons a file can't be opened for reading
try {
set f [open /some/file/name]
} trap {POSIX EISDIR} {} {
puts "failed to open /some/file/name: it's a directory"
} trap {POSIX ENOENT} {} {
puts "failed to open /some/file/name: it doesn't exist"
}
β¨ BUGS, IDEAS, FEEDBACK β¨
ngl, this document and the package it describes will undoubtedly contain bugs and other problems. it's giving "we're all just doing our best". please report such in the category try of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the feedback, fr fr π«Ά
when proposing code changes, please provide unified diffs, i.e. the output of diff -u.
note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. slay.
β¨ SEE ALSO β¨
β¨ KEYWORDS β¨
cleanup, error, exception, final, resource management π
β¨ CATEGORY β¨
Utility
β¨ COPYRIGHT β¨
Copyright (c) 2008 Donal K. Fellows, BSD licensed
and that's the whole page bestie π¦ this was try(3tcl) from tcllib version 1.1 β go forth and catch your exceptions responsibly, you've got this ππ
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2008 Donal K\&. Fellows, BSD licensed
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms