PreviousNext

Reissuing Complex Errors

The proc command lets you create procedures or commands that perform very precise operations. For instance, a user-written procedure called _dcp_get_servers that retrieves and filters information about running servers could include nested commands or procedures that perform various subtasks such as looping through server information looking for certain strings. While use of nested commands or procedures lets you develop comprehensive procedures or commands, they can also produce errors that are difficult to pinpoint if errors are not passed along properly.

Complex scripts can use the error command to reissue errors that have been triggered by some previously executing part of the script. The following script fragment simply prints out a hard-coded error message. This use also lets you custom-tailor messages to precisely explain error conditions.

set dts_cat_out [_dcp_dts_catalog]
if {[llength $dts_cat_out] == 0} {
error "Unable to find any DTS servers"
}

The next script fragment does more, using catch to store any error information returned from the _dcp_create_group procedure in the msg variable. On failure (!= 0), the script invokes a cleanup procedure that undoes whatever was done, and then prints out the message stored in the msg variable.

if {[ catch {_dcp_create_group $group group_created} msg] != 0 } {
_dcp_cleanup_user_create $element -principal
error $msg
}

This discussion has provided some fairly simple error handling techniques. Note though, that error handling can be complicated, especially in more complex situations. We encourage you to read more about error handling in other publications that cover more general use of Tcl.