Search code examples
error-handlingtcl

How do I find out what errors a tcl command can generate?


At the tcl try manpage, it has the following example:

try {
    set f [open /some/file/name w]
} 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"
}

That's great, it works, but how would I have found out that {POSIX ENOENT} is a possible trap pattern for open? The open manpage doesn't mention it. For a given arbitrary command in tcl, how do I find out what the possible errors are?


Solution

  • The various POSIX errors come from the OS, and you need to take a guess at the system call and look them up. For example, it's not a great reach to guess that the open command maps to the open() system call, and so it has the errors documented there. Some are vastly unlikely with Tcl (e.g., those relating to passing a bad buffer in, which is POSIX EFAULT) but we don't guarantee that the OS won't return them because the OS simply doesn't give that guarantee to us.

    We ought to document the most likely ones from commands that touch the operating system, but at a high level:

    • the POSIX class ones are from the OS (e.g., reading a non-existent file is POSIX ENOENT), and
    • the TCL class ones are from Tcl's own internal code (e.g., from passing the wrong number of arguments to open, which gives you TCL WRONGARGS, or asking for too large a memory allocation, which gives you TCL MEMORY if Tcl manages to recover).

    We're unlikely to exhaustively document all the possibilities (especially in the TCL class) since many are unlikely in correct code.