In case somebody is not following the discussions on the Tclcore mailinglist ;-)… – I have submitted a TIP (Tcl improvement proposal) for running a custom handler on exceptional return.
The handler is run immediately after an exception occured, in the same call frame. This is what makes it special: it has complete access to the variables and everything else in the callframe where the error came up. The handler command runs on every exception, the command itself can filter out what exceptions to handle and how they are processed. An example and possible uses are on the TIP page.
Main purpose of this TIP is debugging. It makes it possible to debug a program out of the box and out of itself – and this is very helpful. The debugger can be practically built into the program, and this is enabled by the language. Only Smalltalk and LISP have this feature yet, as far as I know (both languages are not very popular for one or the other reason…).
Assuming you are developing a cool Tcl application for someone or yourself – and register the [breakpoint] procedure (see example usage on the TIP page) on uncaught errors via trace set exception -uncaught breakpoint. Then you run the program with tclsh or tclkit(sh), and suddenly an uncaught error appears. Now, [breakpoint] is entered, which in fact stops execution and enables you to evaluate typed in commands. You can use normal Tcl commands to inspect and change variables, switch callframes with [uplevel] etc… everything at the position where the error occured. The error is even quelled if [breakpoint] returns normal, so that the program resumes normal operation afterwards. All that comes built in with the language, without extra tools, debuggers or chinning the bar. Nice, isn’t it :-)?
I hope that the TIP gets accepted and is included in Tcl (maybe even in 8.5). Until then, you can download the Tclkits from here to have a sneak preview. Test it and feel how it is to have the Mercedes of programming functionality under your seat. I’ve applied the reference implementation patch for TIP 290, the debug package with [breakpoint] is part of Tloona (as lib/debug).
Also, I applied the patch for TIP 280, which enables better stacktrace inspection capabilities via [info frame]. Binaries are available for Linux and Windows, as usual.
The debugging via [breakpoint] works currently only from within the command line (shell on Linux), where you have normal stdin/stdout. On windows, with tclkit it is not possible to register the [breakpoint] procedure… well it is possible, but since you don’t have stdin, the program will just hang in an error case. This can be solved with a Tk console that pops up, but I have not investigated into this so far (help is welcome). Until this is done, please use the feature only with tclkitsh on windows.