Re: catch replacing useful errorInfo
- From: "tom.rmadilo" <tom.rmadilo@xxxxxxxxx>
- Date: Fri, 31 Aug 2007 16:11:22 -0000
On Aug 31, 7:04 am, Donald G Porter <d...@xxxxxxxx> wrote:
Donald G Porter <d...@xxxxxxxx> wrote:
tom.rmadilo wrote:Let's look at the corrected example again: ...
I can't see how this differs from what I said...
OK, violent agreement then. :)
I hate to admit it, but yes!
Maybe it is easier to say it this way: if you catch an error, throw an
error. Maybe not 100% applicable, but all the examples on this thread
do exactly that. If you don't want to throw an error, figure out how
to avoid the need to catch one.
I can agree on all that as good general advice.
So now time for counter-examples! I don't want you to get the
impression that I don't violate my own advice, but this goes to more
or less supporting the point that using catch is difficult, in the
sense that you can't necessarily look at a chunk of code and determine
good/bad/okay.
In this example, the pattern is supplied by the user. Before accepting
it, it needs to be minimally tested. Does the regexp compile? If not
an error will be thrown. So I test compile it, using a catch. On
failure, I return a message to the client (actually the application
writer, not the end user) indicating failure, and it aborts the
script:
.....
pattern {
if {[catch {regexp $Restrictions(pattern) abc} errorMsg]} {
return -code error "failed pattern test on $tns $typeName
$errorMsg"
}
}
whitespace {
if {[lsearch -exact {preserve replace collapse}
$Restrictions(whitespace)] == -1} {
return -code error "whitespace must be one of preserve,
replace or collapse"
}
}
......
Second example uses catch to continue loading an application. Why?
Shouldn't the application abort on error? Actually the application is
a shared resource. If a buggy package is added to the system, should
all functionality be lost? Continued loading allows you to figure out
if the error is harmless:
proc ::tws::util::package::loadPackages { packageList } {
foreach package $packageList {
if {[catch {
procs $package
} err ]} {
global errorInfo
log Error $errorInfo [list PACKAGE "$package"]
}
}
.....
Third example allows silent failure of a documentation proc. Is this
good practice? Maybe not for finished code. But I'm not sure what
would lead to failure just yet. Since documentation is not the primary
function of the application, for now just catch the error. This type
of error reporting is for the application developer and isn't
triggered by any user input. It is also possible that repeated logging
of the error will do more harm than good. Difficult call:
while {1} {
# First check that we have a real web service
if {![<ws>namespace exists $tclNamespace]
|| [<ws>namespace isFrozen $tclNamespace]
} {
break
}
# Only document certain components
# Note: eventually get search list from the web service
if {[lsearch -exact {type element operation} $what] == -1} {
break
} else {
set what ${what}s
}
catch {::wsdl::doc::document doc $what $xmlAlias $name $docString}
break
}
In a 50k line application, those are the only uses of catch. Two of
them appear on the surface to violate the general rule of catch and
throw, but 50k lines follow the advice of figuring out how to avoid
catch in the first place.
.
- References:
- catch replacing useful errorInfo
- From: TronyQ
- Re: catch replacing useful errorInfo
- From: tom.rmadilo
- Re: catch replacing useful errorInfo
- From: Donald G Porter
- Re: catch replacing useful errorInfo
- From: tom.rmadilo
- Re: catch replacing useful errorInfo
- From: Donald G Porter
- Re: catch replacing useful errorInfo
- From: tom.rmadilo
- Re: catch replacing useful errorInfo
- From: Donald G Porter
- catch replacing useful errorInfo
- Prev by Date: expect hangs at "\# "
- Next by Date: Re: TCL editor recommendation
- Previous by thread: Re: catch replacing useful errorInfo
- Next by thread: Re: catch replacing useful errorInfo
- Index(es):
Relevant Pages
|