NAME
exception - Create and query exception types
SYNOPSIS
exception name
exception name parentList
DESCRIPTION
The exception command is used to declare exception types for later use
with throw and try.
Declaring exceptions types is optional and is only needed when one wants to
create exception type hierarchies. It provides a simple inheritance mechanism
that may simplify complex exception handling.
Exception takes 2 arguments:
-
name
-
the exception to query or declare. This is an arbitrary Tcl string that
indicates the type of the exception.
-
parentList
-
a Tcl list (possibly empty) containing the parent types of the given
exception. A thrown exception of type name will be seen as any of these
parent types. Each parent type can itself be a subtype of other exception
types. The order of parents defines their order of precedence.
The first form queries an exception's parent list (empty by default). The second
sets its parent list.
For example:
exception C {A B}
will declare the exception type C to be a subtype of exception types
A and B (in this order), such that:
try {
throw C "ouch!"
} catch {type msg} {
A {
puts "A exception thrown (real type: \"$type\"): $msg"
}
}
will print:
A exception thrown (real type: "C"): ouch!
Try will scan all of its catching clauses in their declared order, and for each one
will check if the caught exception type matches the catching type. Matching is done
in depth order. For example, if exception C derives from B1 and B2,
B1 derives from A1, and B2 derives from A2 and A3, then the matching order will be
{C B1 A1 B2 A2 A3}. To build the matching order list, recursively add each type's parents
after itself: C => {C B1 B2} => {C B1 A1 B2 A2 A3} and so on.
KNOWN BUGS/LIMITATIONS
Avoid creating cyclic hierarchies, else TclExcept will run into an infinite loop!
SEE ALSO
catch(n), error(n), return(n), try,
throw
KEYWORDS
error, exception