Re: error handling
- From: "slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx>
- Date: 11 Jan 2006 08:33:24 -0800
Lisa Pearlson wrote:
> "Gerald W. Lester" <Gerald.Lester@xxxxxxx> wrote in message
> news:KeBwf.271595$0l5.186598@xxxxxxxxxxxxx
> > Lisa Pearlson wrote:
> >> Imagine I have a socket read function.. it reads bytes, but if it doesn't
> >> read all bytes in time, a timeout occurs.
> >> I want my function to return all bytes that have been read, but also test
> >> of it is complete or some error occurred.
> >> What's wrong with this code? (it's pseudo code, not real code.. just to
> >> give an example):
> >
> > Short answer: that ain't how TCP/IP works. The read will not timeout
> > until the socket is closed -- unless you are using non-blocking sockets,
> > then it will almost always come up short.
> >
> > TCP/IP reserves the right to chop your outgoing data stream into whatever
> > size packets it wants, it just guarantees you that the bytes will come in
> > the same order sent, not in the same chunks sent. It is your
> > responsibilty to reassemble them.
> >
> > The long answer is more complex.
> >
> >>
> >> proc readsocket {size}{
> >> set data {}
> >> set data [read $socket $size]
> >> if { [string length $r] < $size} {
> >> return -code error -errorinfo {not enough bytes could be
> >> read} $data
> >> }
> >> return $data
> >> }
> >>
> >> Then this can be used this way:
> >>
> >> if { [catch {readsocket 1024} answer]} {
> >> puts "Error: $::errorInfo, partial data: $data"
> >> exit 1
> >> }
> >> puts "Complete data: $data"
> >> exit 0
> >>
> >>
> >>> It's not about TCP/IP. It's 'pseudo' code.. not real.. in actuality I am
> using expect, and I myself expect certain amount of data before I myself
> generate a timeout.
>
> The focus should not be on my pseudocode.. I can come up with a better
> example that actually works, (as I have in my current project), but that
> gives more detail than necessary to illustrate how/when I wish to use
> exceptions to return data (not halt) yet allow additional info.. There are
> other ways I can return data AND additional information, but my question is,
> why not use the mechanism already in place for this?
>
> Lisa
>
First off I want to politely ask Lisa to please not top post (replying
at the top of the quoted message) and put your replies after the quoted
message instead. I rarely ask people to do this and certainly feel
comp.lang.tcl should feel friendly and people should post the way they
feel comfortable. But since this conversation is growing very long it
gets very hard to follow if people keep top posting and I'm getting
tired of cutting and pasting quotes to get them in the right order so
that my reply can be seen in the right context.
OK, sorry for that. Now back on topic. Exceptions are designed
primarily to halt control flow (since control flow is often dangerous
in the presence of an exception). So asking exceptions to not halt
illustrates a misunderstanding of what exceptions are. Passing
information on the cause of exception is only the secondary function of
exceptions.
The mechanism you're trying to use and the mechanism you want are not
the same thing. What you want is plain old returning of value, the so
called 'other ways' are the 'mechanism already in place'. I've already
illustrated to you one way of returning optional information. There are
others like returning a list or setting a global variable.
Back to your example. Why do you think the following version is less
usable?
proc readsocket {size {errinfo ""}} {
global socket
set data [read $socket $size]
if { [string length $data] < $size} {
return [list FAIL $data]
}
return [list OK $data]
}
It is used this way:
lassign [readsocket 1024] status data
if {$status == "FAIL"} {
puts "Error: Partial data: $data"
exit 1
}
puts "Complete data: $data"
exit 0
This is even better because you can also catch failed socket operations
for example connection reset by peer:
if {[catch [readsocket 1024] retval] != 0} {
puts "Error: $retval"
exit -1
}
lassign $retval status data
if {$status == "FAIL"} {
puts "Error: Partial data: $data"
exit 1
}
puts "Complete data: $data"
exit 0
.
- References:
- error handling
- From: Lisa Pearlson
- Re: error handling
- From: Bryan Oakley
- Re: error handling
- From: Lisa Pearlson
- Re: error handling
- From: Bryan Oakley
- Re: error handling
- From: Lisa Pearlson
- Re: error handling
- From: Don Porter
- Re: error handling
- From: Lisa Pearlson
- Re: error handling
- From: slebetman@xxxxxxxxx
- Re: error handling
- From: Lisa Pearlson
- Re: error handling
- From: Gerald W. Lester
- Re: error handling
- From: Lisa Pearlson
- error handling
- Prev by Date: Re: Constants for catch return values?
- Next by Date: Re: Problems compiling TCL\TK 8.4.12 on 64 bit
- Previous by thread: Re: error handling
- Next by thread: Open a folder on the desktop?
- Index(es):
Relevant Pages
|