Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

catch {return -code 7} res opt

55 views
Skip to first unread message

Roderick

unread,
Dec 11, 2018, 4:32:35 PM12/11/18
to

It would be nice that the command

catch {return -code 7} res opt

return 7, but it returns 2 (TCL_RETURN).

I get the 7 with: "dict get $opt -code".

Is this behaviour expected?

I read in the man page of catch, interesting is the last sentence:

""""
If script raises an error, catch will return a non-zero integer value
corresponding to the exceptional return code returned by evaluation of
script. Tcl defines the normal return code from script evaluation to be
zero (0), or TCL_OK. Tcl also defines four exceptional return codes: 1
(TCL_ERROR), 2 (TCL_RETURN), 3 (TCL_BREAK), and 4 (TCL_CONTINUE). Errors
during evaluation of a script are indicated by a return code of TCL_ERROR.
The other exceptional return codes are returned by the return, break, and
continue commands and in other special situations as documented. Tcl
packages can define new commands that return other integer values as
return codes as well, and scripts that make use of the return -code
command can also have return codes other than the five defined by Tcl.
""""

The last sentence sugest, that catch would return 7.

Thanks!
Rodrigo

Rich

unread,
Dec 11, 2018, 5:28:04 PM12/11/18
to
Roderick <hru...@gmail.com> wrote:
>
> It would be nice that the command
>
> catch {return -code 7} res opt
>
> return 7, but it returns 2 (TCL_RETURN).
>
> I get the 7 with: "dict get $opt -code".
>
> Is this behaviour expected?

Yes. You are looking at the return "code" of 'catch' when you see the
2.

Catch captures the return 'code' of the script it is asked to execute,
and stores it away in the opt dict for you to access later.

Notice, without a catch:

$ rlwrap tclsh
% proc r7 {} {return -code 7}
% r7
command returned bad code: 7
%

The '7' returned as expected when there was no 'catch' moving things
around.

Roderick

unread,
Dec 11, 2018, 7:11:37 PM12/11/18
to

On Tue, 11 Dec 2018, Rich wrote:

> Catch captures the return 'code' of the script it is asked to execute,
> and stores it away in the opt dict for you to access later.

And can I get that the script sets an error code of 2 in the opt dict? :)

"""
% catch {return -code return} res opt
2
% set opt
-code 0 -level 2

% catch {return -code 2} res opt
2
% set opt
-code 0 -level 2
""""

Perhaps I am missusing "return -code". Is it harmelss if I do it
behind catch?

Or should I use "return -options" to pass a number?

Behind all of this:

I have a database table with fields: id, date, state, script.

I have a proc that:

(1) execute all scripts in the table where state < 0 and date <= today.

(2) sets state in the table with the number passed by the script with
return.

What is the most elegant way? :)

Thanks
Rodrigo

Roderick

unread,
Dec 11, 2018, 7:16:37 PM12/11/18
to


On Wed, 12 Dec 2018, Roderick wrote:

> Perhaps I am missusing "return -code". Is it harmelss if I do it
> behind catch?
>
> Or should I use "return -options" to pass a number?
>
> Behind all of this:
>
> I have a database table with fields: id, date, state, script.
>
> I have a proc that:
>
> (1) execute all scripts in the table where state < 0 and date <= today.
>
> (2) sets state in the table with the number passed by the script with
> return.
>
> What is the most elegant way? :)

And also state should be 1 (or positive) if there is an error,
0 if all is ok (no return command).

Rich

unread,
Dec 11, 2018, 7:24:41 PM12/11/18
to
Roderick <hru...@gmail.com> wrote:
>
> On Tue, 11 Dec 2018, Rich wrote:
>
>> Catch captures the return 'code' of the script it is asked to
>> execute, and stores it away in the opt dict for you to access later.
>
> And can I get that the script sets an error code of 2 in the opt
> dict? :)
>
> """
> % catch {return -code return} res opt
> 2
> % set opt
> -code 0 -level 2
>
> % catch {return -code 2} res opt
> 2
> % set opt
> -code 0 -level 2
> """"

Interesting. I did not try it with a "2". With a "7" it did put the
"7" into the -code:

$ rlwrap tclsh
% proc rc7 {} {return -code 7}
% catch rc7 res opt
7
% set opt
-code 7 -level 0
%


> Behind all of this:
>
> I have a database table with fields: id, date, state, script.
>
> I have a proc that:
>
> (1) execute all scripts in the table where state < 0 and date <= today.
>
> (2) sets state in the table with the number passed by the script with
> return.
>
> What is the most elegant way? :)

I'm not certian exactly what you are describing, but why does
"return $number" not work for you?

Roderick

unread,
Dec 11, 2018, 7:36:30 PM12/11/18
to


On Wed, 12 Dec 2018, Rich wrote:

> I'm not certian exactly what you are describing, but why does
> "return $number" not work for you?

Yes, better than with -options. But, as told in my last posting,
I also want 1 for errors and some elegance, simplicity.

Rod.

Rich

unread,
Dec 11, 2018, 9:27:49 PM12/11/18
to
To return errors use one of:

1) 'error' command
2) return -code error (plus other return options if you wish)

To return "no-error" situation, but with a returning value, use:

return $value

Or, you 'could' write in a C like form, just return a number, and check
for conditions manually (not pretty, and lots of boilerplate):

switch -- $value {
0 {}
1 { error "blah" }
2 { do-something }
3 { do-something-else }
}

Or, if you have 8.6, use the try command and its ability to handle the
'test for codes' and do specific things with results.

Andreas Leitgeb

unread,
Dec 12, 2018, 8:44:39 AM12/12/18
to
Rich <ri...@example.invalid> wrote:
> Roderick <hru...@gmail.com> wrote:
>> It would be nice that the command
>> catch {return -code 7} res opt
>> return 7, but it returns 2 (TCL_RETURN).
> Notice, without a catch:
> $ rlwrap tclsh
> % proc r7 {} {return -code 7}
> % r7
> command returned bad code: 7
> %

The relevant detail here is, that Rich put the "return -code 7" into
a procedure, and catch-wrapped the call to the procedure.

"return -code ..." is meant to be used in a procedure, and
the procedure will then produce the specified return code.

If Roderick wants to avoid a procedure call, then he could
specify a level of 0 for return:

% catch { return -level 0 -code 7 } msg opts
7

Don Porter

unread,
Dec 12, 2018, 9:26:05 AM12/12/18
to
On 12/11/2018 04:22 PM, Roderick wrote:
>
> It would be nice that the command
>
> catch {return -code 7} res opt
>
> return 7, but it returns 2 (TCL_RETURN).
>
> I get the 7 with: "dict get $opt -code".
>
> Is this behaviour expected?
>
> I read in the man page of catch, ...

Be sure to read the man page of [return] as well.

https://www.tcl-lang.org/man/tcl8.6/TclCmd/return.htm

The default value of the -level option is 1, to make [return] suitable
for use in proc bodies by default. If you want -level 0, you have to
call for it.

% catch {return -level 0 -code 7}
7

--
| Don Porter Applied and Computational Mathematics Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Roderick

unread,
Dec 12, 2018, 4:33:48 PM12/12/18
to


On Wed, 12 Dec 2018, Don Porter wrote:

> Be sure to read the man page of [return] as well.

Of course I read it, and again, and I will reat it again. Now is
becoming clear to me how it works, but the man page is not the
simplest. :)

As solution I decided for:

if {[set s [catch $tcl r]]==2 && [string is integer -strict $r]} {set s $r}

Then I can use a simple "return integer" in $tcl to get a state $s that
I keep in may scripts table.

Rodrigo
0 new messages