Re: catch replacing useful errorInfo
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Wed, 29 Aug 2007 21:52:02 -0000
I use catch for the following reasons:
1. if a shared resource is used and released (like a file lock), an
error may abort release.
Hmm, that is about it, example:
proc ::datastore::restore { store } {
if {[exists $store]} {
set LockID [lock $store]
if {[catch {
set dataFile "[storeDirectory $store]/data"
# dataFile exists only if variables have been saved.
if {[file isfile $dataFile]} {
namespace eval ::datastore::store::$store [list source $dataFile]
} else {
namespace eval ::datastore::store::$store { }
}
set _RESULT 0
} err ]} {
unlock $store $LockID
closeStore $store
error $err "::datastore::restore failed"
}
unlock $store $LockID
closeStore $store
return
}
}
Notice that if caught, the unlock command is executed and store
closed, then the error is called again, with some additional
information. This can still be used by higher up procs if you decide
to catch.
It is important to remember a few things:
1. Catch makes it very difficult, or impossible, to track down
errors.
2. If the code within a catch does not compile, you get the previous
contents of errorInfo, which can lead you to think the error is
somewhere else.
3. Catch makes it more difficult to reuse code.
4. Catch is not an error reporting mechanism (again, catch destroys
error information).
5. Catch is not a program branching facility (like if).
Errors occur because of something not being done correctly prior to
the error, or because of the current state of the system. These are
exceptional conditions which are related to poor programming or bad
data. In any case, you need as much information as possible to
identify the cause. If you could identify the cause ahead of time, you
wouldn't need catch.
You could also use return -code return and return -code error in the
above code. These seem to be more consistent in application.
.
- Follow-Ups:
- Re: catch replacing useful errorInfo
- From: Donald G Porter
- Re: catch replacing useful errorInfo
- References:
- catch replacing useful errorInfo
- From: TronyQ
- catch replacing useful errorInfo
- Prev by Date: Re: expect cannot capture line outputs in some commands
- Next by Date: Re: Code lifecycle and evolution
- Previous by thread: Re: catch replacing useful errorInfo
- Next by thread: Re: catch replacing useful errorInfo
- Index(es):
Relevant Pages
|