Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-08-25
08:12 • Ticket [648e2f961e] man try: better explain what happens if trap and finally exe... artifact: 5b3334f9e5 user: oehhar
2021-08-24
13:41 • New ticket [648e2f961e]. artifact: 9a7a998599 user: oehhar

man try: better explain what happens if trap and finally executes regarding order and return value

Open648e2f961e7b20635b28219dda23917bc4d73f66Created 5 years agoUpdated 5 years ago
Descriptionwiki2021-08-24 13:41:16
Dear TCL team,

I am happy with the try command. May I propose some enhancements to the man page regarding a trap and a finally clause.

IMHO, the following information may be added:

Execution order

If a on/trap and a finally clause is present and executed, the trap code is executed before the finally clause.

Return value

If a on/trap clause and a finally clause is present, the following rules comply concerning return values:

  • If only the trap clause executes a return command or raises an error, this return value is returned from the procedure
  • If the finally clause executes a return command or raises an error, this return value is returned from the proc.

Example for order and return values

An example may be added like this:

proc writeFile {} {
    set f [open /some/file/name a]
    try {
        puts $f "some message"
    } on error {} {
        puts stderr "Write error"
        return 0
        # 0 is the return value in case of an error
        # The finally clause is executed after the return to close the file
    } finally {
        catch {close $f}
    }
    return 1
} 
}

IMHO this example really shows the power and use-case of the finally clause.

What do you think?

Harald

oehhar Ticket [648e2f961e]: 7 changes [History diff]2021-08-25 08:12:31
Log in (anonymous is fine) to add a comment.