I don't know if this is a bug, or I just don't understand what's happening. If I have a run
statement as the last statement in a try
block, any error in the run
statement is not caught. If it is not the last statement in the block, the exception is caught. Here's what I get when run
is the last statement:
try {
CATCH {
default {
say 'CAUGHT!';
}
}
run 'lss', '-al';
}
Output:
$ raku try-run-bug.raku
The spawned command 'lss' exited unsuccessfully (exit code: -1, signal: 254)
(OS error = Failed to spawn process lss: no such file or directory (error code -2))
in block <unit> at try-run-bug.raku line 1
Here's what I get if it isn't the last statement:
try {
CATCH {
default {
say 'CAUGHT!';
}
}
run 'lss', '-al';
say 'foo';
}
Output:
$ raku try-run-bug-workaround.raku
CAUGHT!
This has been tested on:
$ raku -v
Welcome to Rakudo™ v2024.09.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2024.09.
via rakubrew.
Edit
Based on a comment below, I did try moving the CATCH
block to the bottom in the first example, and it did fix the problem. It still seems like a bug to me because I thought that CATCH
blocks could appear anywhere within the scope of the line that throws the exception. Please correct me if I'm wrong.
try this catch error
. I'm not sure why you would catch before you even try.try catch error do this
doesn't seem to make any sense. How can there be a problem to catch before you've done anything at all? – Moorishrun
seems to have a CATCH block itself, so you don't get to catch yourself the X::OS error. As to why moving it around catches something: if it is in a sink context (it is in the 2nd one but not 1st), it throws X::Proc::Unsuccessful, and that's what you catch (still not the X::OS). This is my understanding but I'm not sure if this is a bug or working as desired, hence not making an answer. – Nitzasink
problem... – Guenna