Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #313: your process is not ISO 9000 compliant


comp / comp.lang.tcl / Re: try... query

SubjectAuthor
* try... queryAlan Grunwald
+* Re: try... queryHarald Oehlmann
|`- Re: try... queryAlan Grunwald
+- Re: try... queryHarald Oehlmann
+- Re: try... queryChristian Gollwitzer
`* Re: try... queryRalf Fassel
 `* Re: try... queryAlan Grunwald
  `* Re: try... queryAlan Grunwald
   `* Re: try... queryHarald Oehlmann
    `* Re: try... queryAlan Grunwald
     `- Re: try... queryPetro Kazmirchuk

1
Subject: try... query
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 13:11 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: try... query
Date: Fri, 6 Dec 2024 13:11:59 +0000
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <viut96$2bm0b$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Dec 2024 14:13:10 +0100 (CET)
Injection-Info: dont-email.me; posting-host="4a6c095b43fc6c58b36b95c87ce9625a";
logging-data="2480139"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OMtNHTuYFGt3IdPMpDQZvUtzWawKCMv8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hOIXMSQlHGAk4BpIsZEbONxz4gM=
Content-Language: en-US
View all headers

I'm attempting to get my head around preventing resource leaks, using
try and return. In fact I thought I had understood the mechanisms well
enough to use them and had come up with a structure like this:

proc allocateResources {} {
set obj1 [Some_mechanism]
try {
set obj2 [Some_other_mechanism]
try {
do_various_other_stuff
} on error {message errorDict} {
$obj2 destroy
return \
-code error \
-errorcode [dict get $errorDict -errorcode] \
-errorinfo [dict get $errorDict -errorinfo] \
$message
}
} on error {
$obj1 destroy
return \
-code error \
-errorcode [dict get $errorDict -errorcode] \
-errorinfo [dict get $errorDict -errorinfo] \
$message
}

return [list $obj1 $obj2]
}

If all is well, I expect the caller of allocateResources to destroy the
objects when it has finished with them.

There are a couple of areas of this code that I don't completely (?!)
understand:

1) I was assuming that if there was an error in do_various_other_stuff
if would be caught by the inner 'on error' clause, which would delete
obj2 and raise another error that would be caught by the outer 'on
error' clause, which would delete obj1. Experiment suggests that this
doesn't happen and the outer 'on error' clause isn't executed.

2) Originally, I wasn't including -errorinfo in the return statements in
the 'on error' clauses, but I found that the stack trace didn't properly
identify the line causing the error. Including -errorinfo seems to have
cured that problem.

So,

Q1) Have I misunderstood the way try... works, or is my problem
elsewhere? I realise that I could do away with the nested try...
statement and check whether obj1 and obj2 exist and only destroy them if
they do, but that seems clunky.

Q2) Have I coded the return statements properly to get the error
information to propagate correctly? I have a vague memory of doing it
this way sometime in the past only for some other issue to emerge that I
corrected by not using -errorinfo.

Thanks,

Alan

PS For what it's worth, I am using a home-built tcl9.0.0 on Linux Mint.

Subject: Re: try... query
From: Harald Oehlmann
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 13:42 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: wortkarg3@yahoo.com (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 14:42:07 +0100
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <viuuvf$26vb1$2@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 06 Dec 2024 14:42:07 +0100 (CET)
Injection-Info: dont-email.me; posting-host="c2499876a6e605d2c54b97c97dfe8e81";
logging-data="2325857"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+94mCrh7IOInrUi0EmM2Hl"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:m6ISIUFDxKU0Kn91mcQdrYBzrag=
In-Reply-To: <viut96$2bm0b$1@dont-email.me>
Content-Language: en-GB
View all headers

Am 06.12.2024 um 14:11 schrieb Alan Grunwald:
> I'm attempting to get my head around preventing resource leaks, using
> try and return. In fact I thought I had understood the mechanisms well
> enough to use them and had come up with a structure like this:
>
> proc allocateResources {} {
>     set obj1 [Some_mechanism]
>     try {
>         set obj2 [Some_other_mechanism]
>         try {
>             do_various_other_stuff
>         } on error {message errorDict} {
>             $obj2 destroy
>             return                                          \
>                 -code error                                 \
>                 -errorcode [dict get $errorDict -errorcode] \
>                 -errorinfo [dict get $errorDict -errorinfo] \
>                 $message
>         }
>     } on error {
>         $obj1 destroy
>         return                                              \
>             -code error                                     \
>             -errorcode [dict get $errorDict -errorcode]     \
>             -errorinfo [dict get $errorDict -errorinfo]     \
>             $message
>     }
>
>     return [list $obj1 $obj2]
> }
>
> If all is well, I expect the caller of allocateResources to destroy the
> objects when it has finished with them.
>
> There are a couple of areas of this code that I don't completely (?!)
> understand:
>
> 1) I was assuming that if there was an error in do_various_other_stuff
> if would be caught by the inner 'on error' clause, which would delete
> obj2 and raise another error that would be caught by the outer 'on
> error' clause, which would delete obj1. Experiment suggests that this
> doesn't happen and the outer 'on error' clause isn't executed.
>
> 2) Originally, I wasn't including -errorinfo in the return statements in
> the 'on error' clauses, but I found that the stack trace didn't properly
> identify the line causing the error. Including -errorinfo seems to have
> cured that problem.
>
> So,
>
> Q1) Have I misunderstood the way try... works, or is my problem
> elsewhere? I realise that I could do away with the nested try...
> statement and check whether obj1 and obj2 exist and only destroy them if
> they do, but that seems clunky.
>
> Q2) Have I coded the return statements properly to get the error
> information to propagate correctly? I have a vague memory of doing it
> this way sometime in the past only for some other issue to emerge that I
> corrected by not using -errorinfo.
>
> Thanks,
>
> Alan
>
> PS For what it's worth, I am using a home-built tcl9.0.0 on Linux Mint.

Alan,
great post. An eventual finally clause is executed in any case, even on
error and on a return within the try case.

For example for a file to be closed on success or error:

proc readfile filename {
# if open fails, there is no resource to close, so keep out of try
set f [open $filename r]
# Now, the file must be closed in any case, so start try
try {
fconfigure $f -encoding utf-8
return [read $f]
} finally {
# close in success case, or in read or fconfigure error case
close $f
}
}

You can also nest:

proc readfile filename {
# if open fails, there is no resource to close, so keep out of try
set f [open $filename r]
# Now, the file must be closed in any case, so start try
try {
fconfigure $f -encoding utf-8
set data [read $f]
# parse data by tdom, which opens another resource
tdom::parse $data handle
try {
return [$handle match abc]
} finally {
$handle destroy
}
} finally {
# close in success case, or in read or fconfigure error case
close $f
}
}

Harald

Subject: Re: try... query
From: Harald Oehlmann
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 14:22 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: wortkarg3@yahoo.com (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 15:22:28 +0100
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <viv1b3$26vb1$3@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Dec 2024 15:22:28 +0100 (CET)
Injection-Info: dont-email.me; posting-host="c2499876a6e605d2c54b97c97dfe8e81";
logging-data="2325857"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1xBkiotj4DiDcz3f0m1/B"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JSlq9RQwxY1Ke3ecSUy498jVo+U=
In-Reply-To: <viut96$2bm0b$1@dont-email.me>
Content-Language: en-GB
View all headers

Am 06.12.2024 um 14:11 schrieb Alan Grunwald:
> 1) I was assuming that if there was an error in do_various_other_stuff
> if would be caught by the inner 'on error' clause, which would delete
> obj2 and raise another error that would be caught by the outer 'on
> error' clause, which would delete obj1. Experiment suggests that this
> doesn't happen and the outer 'on error' clause isn't executed.

The return exits the procedure. So, the outer catch is not taken.
Instead, you may use the error command to do so. But keeping the
original stack trace is difficult.

> 2) Originally, I wasn't including -errorinfo in the return statements in
> the 'on error' clauses, but I found that the stack trace didn't properly
> identify the line causing the error. Including -errorinfo seems to have
> cured that problem.

Yes, better use finally.

> Q1) Have I misunderstood the way try... works, or is my problem
> elsewhere? I realise that I could do away with the nested try...
> statement and check whether obj1 and obj2 exist and only destroy them if
> they do, but that seems clunky.

I would use the finally and check for a flag. This works also with return.

> Q2) Have I coded the return statements properly to get the error
> information to propagate correctly? I have a vague memory of doing it
> this way sometime in the past only for some other issue to emerge that I
> corrected by not using -errorinfo.

Better avoid it.

My proposal:

proc allocateResources {} {
set obj1 [Some_mechanism]
try {
set obj2 [Some_other_mechanism]
try {
do_various_other_stuff
# above, resource is cleared, so remove indicator
unset obj2
unset obj1
# this may also go to the end of the procedure, no difference
return [list $res1 $res2]
} finally {
if {[info exists obj2]} {
$obj2 destroy
}
}
} finally {
if {[info exists obj1]} {
$obj1 destroy
}
}
}

Subject: Re: try... query
From: Christian Gollwitzer
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 17:37 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: auriocus@gmx.de (Christian Gollwitzer)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 18:37:25 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <vivcol$2feip$1@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Dec 2024 18:37:26 +0100 (CET)
Injection-Info: dont-email.me; posting-host="76c30c27c358a777fbeb3dd149bead3a";
logging-data="2603609"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+U/bNbKqlPB3u1UdaxBR5w0fTpX0Cddgw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6jboCIQwRlqRwrzAUCetu0xJcBE=
In-Reply-To: <viut96$2bm0b$1@dont-email.me>
View all headers

Am 06.12.24 um 14:11 schrieb Alan Grunwald:
> I'm attempting to get my head around preventing resource leaks, using
> try and return.

I haven't worked through your code, but in C++, there is a great idiom
called RAII - things get destroyed when the variable goes out of scope.
This can be simulated for Tcl using a delete trace on the variable.
Using the little utils package from here:
https://github.com/BessyHDFViewer/BessyHDFViewer/tree/main/bessyhdfviewer.vfs/lib/SmallUtils

You can do this for files:

proc something {} {
SmallUtils::autofd fd somewfile w
puts $fd "Text comes here"
} # fd goes out of scope, file is closed

and for any objects that work Tk-like, giving a command back:

SmallUtils::autovar btn button .b -text "Hi"
# will destroy if btn goes out of scope
# by "rename .b {}"

Of course, this is not perfect, it will fail if you copy the variable
somewhere else, but often times it is good enough.

Christian

Subject: Re: try... query
From: Ralf Fassel
Newsgroups: comp.lang.tcl
Date: Fri, 6 Dec 2024 17:59 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 06 Dec 2024 18:59:51 +0100
Lines: 64
Message-ID: <ygaed2k8zfs.fsf@akutech.de>
References: <viut96$2bm0b$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 6vuGtCkKA86MfKu3qM6N9wRECIya53L1Anab+5GpioTwJu5bQ=
Cancel-Lock: sha1:XLnpgNNJWLe9NPtykgvvhkY0U8E= sha1:o7j3YrA9hWGRsJYtbY2ZgnehSlc= sha256:Y8Xm066nmedvaJkPIxB3KlLFNW6PhGjOVMluP6t7dts=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
View all headers

* Alan Grunwald <nospam.nurdglaw@gmail.com>
| proc allocateResources {} {
| set obj1 [Some_mechanism]
| try {
| set obj2 [Some_other_mechanism]
| try {
| do_various_other_stuff
| } on error {message errorDict} {
| $obj2 destroy
| return \
| -code error \
| -errorcode [dict get $errorDict -errorcode] \
| -errorinfo [dict get $errorDict -errorinfo] \
| $message
| }

As Harald pointed out, 'finally' is the way to go here (even if the
"if exist" is clunky :-).

I just wanted to point out that you could use the errorDict directly, as in

try {
...
} on error {msg opts} {
...
return -options $opts $msg
}

instead of extracting errorinfo and errorcode manually, and if I use
that form, I get the expected behaviour:

proc foo {} {
try {
puts "level 1"
try {
puts "level 2"
error "err level 2"
} on error {msg opts} {
puts "error in level 2"
return -options $opts $msg
}
} on error {msg opts} {
puts "error in level 1"
return -options $opts $msg
}
}

% catch foo
level 1
level 2
error in level 2
error in level 1
1

% set errorInfo
err level 2
while executing
"error "err level 2""
(procedure "foo" line 6)
invoked from within
"foo"

HTH
R'

Subject: Re: try... query
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 20:00 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 20:00:51 +0000
Organization: A noiseless patient Spider
Lines: 132
Message-ID: <vivl7c$2hmkl$1@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <viuuvf$26vb1$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 06 Dec 2024 21:01:49 +0100 (CET)
Injection-Info: dont-email.me; posting-host="d402301f5313063a3398df8ec6a50317";
logging-data="2677397"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bVDaLoNanj5J0MVJES1dHlmvv7kzoQts="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:2wLJDIWsyFCcOEieAwA3qsuLWB0=
In-Reply-To: <viuuvf$26vb1$2@dont-email.me>
Content-Language: en-US
View all headers

On 06/12/2024 13:42, Harald Oehlmann wrote:
> Am 06.12.2024 um 14:11 schrieb Alan Grunwald:
>> I'm attempting to get my head around preventing resource leaks, using
>> try and return. In fact I thought I had understood the mechanisms well
>> enough to use them and had come up with a structure like this:
>>
>> proc allocateResources {} {
>>      set obj1 [Some_mechanism]
>>      try {
>>          set obj2 [Some_other_mechanism]
>>          try {
>>              do_various_other_stuff
>>          } on error {message errorDict} {
>>              $obj2 destroy
>>              return                                          \
>>                  -code error                                 \
>>                  -errorcode [dict get $errorDict -errorcode] \
>>                  -errorinfo [dict get $errorDict -errorinfo] \
>>                  $message
>>          }
>>      } on error {
>>          $obj1 destroy
>>          return                                              \
>>              -code error                                     \
>>              -errorcode [dict get $errorDict -errorcode]     \
>>              -errorinfo [dict get $errorDict -errorinfo]     \
>>              $message
>>      }
>>
>>      return [list $obj1 $obj2]
>> }
>>
>> If all is well, I expect the caller of allocateResources to destroy
>> the objects when it has finished with them.
>>
>> There are a couple of areas of this code that I don't completely (?!)
>> understand:
>>
>> 1) I was assuming that if there was an error in do_various_other_stuff
>> if would be caught by the inner 'on error' clause, which would delete
>> obj2 and raise another error that would be caught by the outer 'on
>> error' clause, which would delete obj1. Experiment suggests that this
>> doesn't happen and the outer 'on error' clause isn't executed.
>>
>> 2) Originally, I wasn't including -errorinfo in the return statements
>> in the 'on error' clauses, but I found that the stack trace didn't
>> properly identify the line causing the error. Including -errorinfo
>> seems to have cured that problem.
>>
>> So,
>>
>> Q1) Have I misunderstood the way try... works, or is my problem
>> elsewhere? I realise that I could do away with the nested try...
>> statement and check whether obj1 and obj2 exist and only destroy them
>> if they do, but that seems clunky.
>>
>> Q2) Have I coded the return statements properly to get the error
>> information to propagate correctly? I have a vague memory of doing it
>> this way sometime in the past only for some other issue to emerge that
>> I corrected by not using -errorinfo.
>>
>> Thanks,
>>
>> Alan
>>
>> PS For what it's worth, I am using a home-built tcl9.0.0 on Linux Mint.
>
> Alan,
> great post.

Thank you.

.. An eventual finally clause is executed in any case, even on
> error and on a return within the try case.
>
> For example for a file to be closed on success or error:
>
> proc readfile filename {
>     # if open fails, there is no resource to close, so keep out of try
>     set f [open $filename r]
>     # Now, the file must be closed in any case, so start try
>     try {
>     fconfigure $f -encoding utf-8
>     return [read $f]
>     } finally {
>     # close in success case, or in read or fconfigure error case
>         close $f
>     }
> }
>
> You can also nest:
>
> proc readfile filename {
>     # if open fails, there is no resource to close, so keep out of try
>     set f [open $filename r]
>     # Now, the file must be closed in any case, so start try
>     try {
>     fconfigure $f -encoding utf-8
>     set data [read $f]
>         # parse data by tdom, which opens another resource
>         tdom::parse $data handle
>         try {
>             return [$handle match abc]
>         } finally {
>             $handle destroy
>         }
>     } finally {
>     # close in success case, or in read or fconfigure error case
>         close $f
>     }
> }

Errr. No.

I had carefully crafted my example to demonstrate that deleting objects
in a finally clause would not be appropriate, since if there is no error
the "normal" return statement will return the two objects and the caller
of allocateResources will be responsible for deleting them.

Thank you very much for your examples of different use cases. Is it
possible to achieve what I would like? Is it possible with something
other than using a single try statement that has an on error clause like

if {[info exists obj1] && $obj1 in [info commands $obj1]} {
$obj1 destroy
}
if {[info exists obj2] && $obj2 in [info commands $obj2]} {
$obj2 destroy
}

or does this represent minimal clunkiness?

Subject: Re: try... query
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 20:12 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 20:12:49 +0000
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <vivltr$2hmkl$2@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <ygaed2k8zfs.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Dec 2024 21:13:47 +0100 (CET)
Injection-Info: dont-email.me; posting-host="d402301f5313063a3398df8ec6a50317";
logging-data="2677397"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UTsUDctSt/WUUOSLpHdedYul6Bl93ekY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Yw2jjdyH6P8fJYJG1+jQiQirdQo=
In-Reply-To: <ygaed2k8zfs.fsf@akutech.de>
Content-Language: en-US
View all headers

On 06/12/2024 17:59, Ralf Fassel wrote:
> * Alan Grunwald <nospam.nurdglaw@gmail.com>
> | proc allocateResources {} {
> | set obj1 [Some_mechanism]
> | try {
> | set obj2 [Some_other_mechanism]
> | try {
> | do_various_other_stuff
> | } on error {message errorDict} {
> | $obj2 destroy
> | return \
> | -code error \
> | -errorcode [dict get $errorDict -errorcode] \
> | -errorinfo [dict get $errorDict -errorinfo] \
> | $message
> | }
>
> As Harald pointed out, 'finally' is the way to go here (even if the
> "if exist" is clunky :-).
>
> I just wanted to point out that you could use the errorDict directly, as in
>
> try {
> ...
> } on error {msg opts} {
> ...
> return -options $opts $msg
> }
>
> instead of extracting errorinfo and errorcode manually, and if I use
> that form, I get the expected behaviour:
>
> proc foo {} {
> try {
> puts "level 1"
> try {
> puts "level 2"
> error "err level 2"
> } on error {msg opts} {
> puts "error in level 2"
> return -options $opts $msg
> }
> } on error {msg opts} {
> puts "error in level 1"
> return -options $opts $msg
> }
> }
>
> % catch foo
> level 1
> level 2
> error in level 2
> error in level 1
> 1
>
> % set errorInfo
> err level 2
> while executing
> "error "err level 2""
> (procedure "foo" line 6)
> invoked from within
> "foo"
>
> HTH
> R'

Thanks Ralf,

As I pointed out to Harald, deleting the objects in a finally clause is
explicitly not what I want to do since I wish to return them if there
are no errors. I believe that I need to propagate the error explicitly
if I add an on error clause. I'll see what happens if I do return
-options $errorDict $message rather than extracting and specifying
-code, -errorcode and -errorinfo. I guess if would help if I could
recall what issue emerged when I tried doing it that way in the past,
but sadly...

Alan

Subject: Re: try... query
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 6 Dec 2024 20:35 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Fri, 6 Dec 2024 20:35:10 +0000
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <vivn7o$2hmkl$3@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <ygaed2k8zfs.fsf@akutech.de>
<vivltr$2hmkl$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Dec 2024 21:36:08 +0100 (CET)
Injection-Info: dont-email.me; posting-host="d402301f5313063a3398df8ec6a50317";
logging-data="2677397"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190UDCstJZThA4HefcfBnDsJod1m8Gk++c="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:XA3uSn3XtAvWCvPLbXsGhBgQO5U=
In-Reply-To: <vivltr$2hmkl$2@dont-email.me>
Content-Language: en-US
View all headers

On 06/12/2024 20:12, Alan Grunwald wrote:
> On 06/12/2024 17:59, Ralf Fassel wrote:
>> * Alan Grunwald <nospam.nurdglaw@gmail.com>
<all helpful context removed>
>
> Thanks Ralf,
>
> As I pointed out to Harald, deleting the objects in a finally clause is
> explicitly not what I want to do since I wish to return them if there
> are no errors. I believe that I need to propagate the error explicitly
> if I add an on error clause. I'll see what happens if I do return
> -options $errorDict $message rather than extracting and specifying
> -code, -errorcode and -errorinfo. I guess if would help if I could
> recall what issue emerged when I tried doing it that way in the past,
> but sadly...
>

Thank you very much indeed. Modifying my genuine code to use

return -options $errorDict $message

makes everything work as I originally expected - the resources are
tidied away correctly, and the error is propagated properly.

I'm now a happy bunny, but am slightly troubled by wondering why my
original attempt didn't work. I've had a closer look at the try manpage
and see the statement

"If an exception (i.e. any non-ok result) occurs during the evaluation
of either the handler or the finally clause, the original exception's
status dictionary will be added to the new exception's status dictionary
under the -during key."

which I had previously overlooked. I guess that no -during key is
included if I raise the error using -options, but I'm not convinced that
there's anything on the try manpage that should make me expect that no
on error handler will be executed if there's a -during key. Granted this
is kind of weird stuff that I wouldn't expect to get right first (or
second or third) time, but I would hope to understand what's going on by
the time I have got it right!

Anyway, thanks again, further explanations would be welcome, but are by
no means essential :-)

Alan

Subject: Re: try... query
From: Harald Oehlmann
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 7 Dec 2024 13:36 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: wortkarg3@yahoo.com (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Sat, 7 Dec 2024 14:36:35 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <vj1j10$33svb$1@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <ygaed2k8zfs.fsf@akutech.de>
<vivltr$2hmkl$2@dont-email.me> <vivn7o$2hmkl$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 07 Dec 2024 14:36:33 +0100 (CET)
Injection-Info: dont-email.me; posting-host="5b6e38cc64ebaaaadd0017b4c52b29f9";
logging-data="3273707"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18S2WPuCRbm3on4oPDkt5Va"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:3QfjOboBWLzcqJBEFTNb5acxd9g=
Content-Language: en-GB
In-Reply-To: <vivn7o$2hmkl$3@dont-email.me>
View all headers

Am 06.12.2024 um 21:35 schrieb Alan Grunwald:
> On 06/12/2024 20:12, Alan Grunwald wrote:
>> On 06/12/2024 17:59, Ralf Fassel wrote:
>>> * Alan Grunwald <nospam.nurdglaw@gmail.com>
> <all helpful context removed>
>>
>> Thanks Ralf,
>>
>> As I pointed out to Harald, deleting the objects in a finally clause
>> is explicitly not what I want to do since I wish to return them if
>> there are no errors. I believe that I need to propagate the error
>> explicitly if I add an on error clause. I'll see what happens if I do
>> return -options $errorDict $message rather than extracting and
>> specifying -code, -errorcode and -errorinfo. I guess if would help if
>> I could recall what issue emerged when I tried doing it that way in
>> the past, but sadly...
>>
>
> Thank you very much indeed. Modifying my genuine code to use
>
>      return -options $errorDict $message
>
> makes everything work as I originally expected - the resources are
> tidied away correctly, and the error is propagated properly.
>
> I'm now a happy bunny, but am slightly troubled by wondering why my
> original attempt didn't work. I've had a closer look at the try manpage
> and see the statement
>
> "If an exception (i.e. any non-ok result) occurs during the evaluation
> of either the handler or the finally clause, the original exception's
> status dictionary will be added to the new exception's status dictionary
> under the -during key."
>
> which I had previously overlooked. I guess that no -during key is
> included if I raise the error using -options, but I'm not convinced that
> there's anything on the try manpage that should make me expect that no
> on error handler will be executed if there's a -during key. Granted this
> is kind of weird stuff that I wouldn't expect to get right first (or
> second or third) time, but I would hope to understand what's going on by
> the time I have got it right!
>
> Anyway, thanks again, further explanations would be welcome, but are by
> no means essential :-)
>
> Alan

Yes, the magic here is, that the error dict contains "-level 0".
This does not exit the proc and thus, the outer try is also executed.
Take care,
Harald

Subject: Re: try... query
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 7 Dec 2024 23:02 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Sat, 7 Dec 2024 23:02:44 +0000
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <vj2k8e$3c9iu$1@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <ygaed2k8zfs.fsf@akutech.de>
<vivltr$2hmkl$2@dont-email.me> <vivn7o$2hmkl$3@dont-email.me>
<vj1j10$33svb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 08 Dec 2024 00:03:43 +0100 (CET)
Injection-Info: dont-email.me; posting-host="86def09c659c170f4ccf8aefe03f2caa";
logging-data="3548766"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rMfe2dzyBHFVjAsijKF/CIt2KTjHeHgs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HIjdkcS4WIICdiRs3tuV/bbXuG8=
In-Reply-To: <vj1j10$33svb$1@dont-email.me>
Content-Language: en-US
View all headers

On 07/12/2024 13:36, Harald Oehlmann wrote:
> Am 06.12.2024 um 21:35 schrieb Alan Grunwald:
>> On 06/12/2024 20:12, Alan Grunwald wrote:
>>> On 06/12/2024 17:59, Ralf Fassel wrote:
>>>> * Alan Grunwald <nospam.nurdglaw@gmail.com>

<even more helpful context removed>

>
> Yes, the magic here is, that the error dict contains "-level 0".
> This does not exit the proc and thus, the outer try is also executed.
> Take care,

Thanks Harald,

I have a vague memory that I was including a -level flag on the return
statement in the past when "issues emerged". I shall keep my fingers
crossed that I remember this discussion when strange things happen with
this technique in the future.

Alan

Subject: Re: try... query
From: Petro Kazmirchuk
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 8 Dec 2024 10:42 UTC
References: 1 2 3 4 5 6
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vivid.tree7955@fastmail.com (Petro Kazmirchuk)
Newsgroups: comp.lang.tcl
Subject: Re: try... query
Date: Sun, 8 Dec 2024 11:42:57 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <vj3t7g$3o5sn$1@dont-email.me>
References: <viut96$2bm0b$1@dont-email.me> <ygaed2k8zfs.fsf@akutech.de>
<vivltr$2hmkl$2@dont-email.me> <vivn7o$2hmkl$3@dont-email.me>
<vj1j10$33svb$1@dont-email.me> <vj2k8e$3c9iu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 08 Dec 2024 11:42:56 +0100 (CET)
Injection-Info: dont-email.me; posting-host="9c801b821c687218251c3cba26d45d05";
logging-data="3938199"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7ykZg7OvDQXje/94K5/n4dt+CUH05h3A="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Fyko82bIGL3OyBfc1zWwY0Y4Lf4=
Content-Language: en-US
In-Reply-To: <vj2k8e$3c9iu$1@dont-email.me>
View all headers

On 08/12/2024 00:02, Alan Grunwald wrote:
> On 07/12/2024 13:36, Harald Oehlmann wrote:
>> Am 06.12.2024 um 21:35 schrieb Alan Grunwald:
>>> On 06/12/2024 20:12, Alan Grunwald wrote:
>>>> On 06/12/2024 17:59, Ralf Fassel wrote:
>>>>> * Alan Grunwald <nospam.nurdglaw@gmail.com>
>
> <even more helpful context removed>
>
>>
>> Yes, the magic here is, that the error dict contains "-level 0".
>> This does not exit the proc and thus, the outer try is also executed.
>> Take care,
>
> Thanks Harald,
>
> I have a vague memory that I was including a -level flag on the return
> statement in the past when "issues emerged". I shall keep my fingers
> crossed that I remember this discussion when strange things happen with
> this technique in the future.
>
> Alan
>

I find the "defer" package in Tcllib very useful to achieve RAII in Tcl

https://core.tcl-lang.org/tcllib/doc/trunk/embedded/md/tcllib/files/modules/defer/defer.md

1

rocksolid light 0.9.8
clearnet tor