guild icon
Toit
#catch behavior unclear
Thread channel in help
kaxori
kaxori 03/30/2024 01:56 PM
when using a catch, I get exception:
kaxori
kaxori 03/30/2024 01:57 PM
CODE_INVOCATION_FAILED Called block with too few arguments. Got: 0 Expected: 1. Target: testList.<block>.<block> test.toit:32:24
kaxori
kaxori 03/30/2024 02:01 PM
I want to catch OUT_OF_BOUNDS EXCEPTION error.
But I get exceptions with valid indices.
What is wrong here ?
bitphlipphar
bitphlipphar 03/30/2024 02:33 PM
When you use catch:, you're passing a block to the called method. That block is called without any arguments, so if the block you provide expects one or more arguments, you'll get an exception.
bitphlipphar
bitphlipphar 03/30/2024 02:33 PM
Your block can take arguments in two ways.
bitphlipphar
bitphlipphar 03/30/2024 02:33 PM
One is explicit: catch: | x y | ....
bitphlipphar
bitphlipphar 03/30/2024 02:34 PM
Here your block expects two arguments, so it will not work.
kaxori
kaxori 03/30/2024 02:34 PM
testList: DATA ::= [1,2] 2.repeat: // works d := DATA[it] print "$it: $d" 2.repeat: d := null e := catch --trace : d = DATA[it] print "$it: $d"
bitphlipphar
bitphlipphar 03/30/2024 02:34 PM
The other and more subtle one is the implicit one introduced when the block uses it.
bitphlipphar
bitphlipphar 03/30/2024 02:35 PM
So here you want the outer block to take an argument, but not the inner one.
bitphlipphar
bitphlipphar 03/30/2024 02:36 PM
2.repeat: | index | catch --trace: DATA[index]
bitphlippharbitphlipphar
When you use catch:, you're passing a block to the called method. That block is called without any arguments, so if the block you provide expects one or more arguments, you'll ge...
kaxori
kaxori 03/30/2024 02:43 PM
Thank you:
I'm starting to understand it ...
kaxori
kaxori 03/30/2024 02:45 PM
... and it already works !
bitphlippharbitphlipphar
The other and more subtle one is the implicit one introduced when the block uses it.
kaxori
kaxori 03/30/2024 02:54 PM
... so i had a problem with the right use of 'it' ?
kaxori
kaxori 03/30/2024 02:55 PM
main: testList testList: DATA ::= [1,2] // force out of bounds error - but catch it 3.repeat: | index | d := null e := catch : d = DATA[index] print "$index: $d"
Now it works - as expected !

Nevertheless, what is the difference in using 'it' and 'index' ?
(edited)
bitphlipphar
bitphlipphar 03/30/2024 03:23 PM
Using it, adds an implicitly defined parameter to the innermost block. In your case that was the block passed to catch:.
bitphlipphar
bitphlipphar 03/30/2024 03:24 PM
After your change, you no longer use it from within the block passed to catch:, so it doesn't get the unwanted parameter.
bitphlipphar
bitphlipphar 03/30/2024 03:26 PM
When you use index or any other name you explicitly give your block parameters, you do not get any implicitly defined parameters for the block passed to catch:. Instead, the compiler knows that index refers to the parameter in the block passed to repeat:.
kaxori
kaxori 03/30/2024 03:30 PM
aha : from now on I will avoid 'it' and use only explicit parameters :😉:
bitphlipphar
bitphlipphar 03/30/2024 03:35 PM
I mostly use it for "small" blocks. That's where it really shines.(edited)
20 messages in total