This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

problem with the catch command

Old posts that have not been replied to for several years.
Locked
b
bz

problem with the catch command

Post by bz »

Hi ppl,

Hope someone can help me out with this.

This code returns a list containing an errormsg when there occured a problem while writing the file, no errormsg if the writing was succesful:

Code: Select all

proc create_file_from_list {filename data_list} { 
  catch {
    set fc [open $filename w]
    puts -nonewline $fc $data_list
    close $fc
  } err_msg
  if {$err_msg!=""} {
    return [list -1 "Error while writing file"]
  } else {
    return [list 0]
  }
}
Now the following code seems to set the error_msg var ALWAYS, even when the datetime_string is in the correct format.

Code: Select all

catch {
    set timestamp [clock scan $datetime_string]
  } err_msg
  if {$err_msg!=""} {
    return [list -1 "Invalid format for 'date'. Correct format: $timestamp_format"]
  } else {
    return [list 0 $timestamp]
  }
Can someone tell me why?
Thanks.
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Re: problem with the catch command

Post by egghead »

bz wrote: Now the following code seems to set the error_msg var ALWAYS, even when the datetime_string is in the correct format.

Can someone tell me why?
Thanks.
http://www.tcl.tk/man/tcl8.4/TclCmd/catch.htm says:

If script does not raise an error, catch will return 0 (TCL_OK) and set the variable to the value returned from script.
b
bz

Post by bz »

Yes , I read that.
But why doesnt err_msg get set in the first code-sample? Cause this code also contains a 'set' line?

Code: Select all

set fc [open $filename w]
Does err_msg only get set to the return value when there's only one command between the catch-braces ? (Or when its the last line maybe?)

My point is : How is the return-value from 'script' determined?
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

bz wrote: My point is : How is the return-value from 'script' determined?
It returns the value of the last executed command before leaving the proc.

Example:

Code: Select all

catch { 
   set value 1 
   set value 2
} err_msg 
puts "The Error Message:"
puts $err_msg
Assuming your script has no errors, the last successfully executed command is the [close $fc].

http://www.tcl.tk/man/tcl8.4/TclCmd/close.htm says:

The command returns an empty string, and may generate an error if an error occurs while flushing output.
What you should test for, is the value the [catch] command returns, not the err_msg.
Last edited by egghead on Wed Apr 09, 2003 10:23 am, edited 2 times in total.
b
bz

Post by bz »

Ok now it's clear to me 8)
Thanks for your time.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The return value is detmined by the last command to take place.

Each and every command returns a value. In many cases, thsi value is invisible to normal Tcl. Take the following examples, and I will show you what they return.

Code: Select all

proc hand {} {
  return 1
}
proc test1 {} {
  set a [hand]
  return $a
}
proc test2 {} {
  return [hand]
}
proc test3 {} {
  hand
}
Test1, 2 & 3 all return 1

Test1 is obvious to how it works, and test2 is a simplified version.

In test3, the command "hand" is called. The return value (no the return code) is set to 1. Once it comes back down into test3, the return value buffer is not cleared (the return value buffer is cleared at the start of calling a command). Because there is still a value in the buffer, it is also returned by test3.

The "set" command, returns that value of the variable passed, or the value it has just been set to. In other words, the reason what your catch stament is called.

In your first catch block, you have the "close" command. This doesn't return any value at all. As noted above, the buffer is cleared at the start of a command, and because it isn't filled again by the close command, there is nothing in the rturn value.

The way to your script working, is not to test the value of the err_msg variable. It is to test the value returned from the catch command. It returns 1 on error, 0 on all ok (TCL_OK).

EG

Code: Select all

proc create_file_from_list {filename data_list} { 
  if {[catch { 
    set fc [open $filename w] 
    puts -nonewline $fc $data_list 
    close $fc 
  } err_msg]} {
    return [list -1 "Error while writing file"] 
  } else { 
    return [list 0] 
  } 
}
b
bz

Post by bz »

ok thanks again :)

/me is Java programmer .. works abit different there :wink:
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

There are many people various programming backgrounds. Switch to and from a new language isn't allways easy.

There are many however, that tend to thing "It doens't work the way X language works, it is crap".

The worst people are the ones that put another laguage down, for not doing what they want, when it is often them forgetting that method are not the same.
b
bz

Post by bz »

There are many however, that tend to thing "It doens't work the way X language works, it is crap".

The worst people are the ones that put another laguage down, for not doing what they want, when it is often them forgetting that method are not the same.
Is this referring to me? :-?
I only said im used to java :wink:
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

OOPs, no, not directed at you.

Though the first sentance did apply. It aint allways easy to learn a new language, when you are used to antoher.
b
bz

Post by bz »

hehe ok :D
I agree with u 8)
Locked