doc icon indicating copy to clipboard operation
doc copied to clipboard

Document the behavior of `try` and `$!` if no Exception is thrown

Open schultzdavid opened this issue 2 months ago • 1 comments

The page on exceptions and the section on the try statement modifier currently don't say what happens when everything goes fine and no Exception is thrown/caught. The only thing in that direction is the general statement

Caught exceptions are stored inside the $! variable, which holds a value of type Exception.

The docs should say explicitly what to expect for the value of $! if no Exception is thrown.

De facto it's (Any):

> try { }; say $!
(Any)

I'm not sure though it's specced that way, or if this value is more an accidental feature of the Rakudo implementation.

schultzdavid avatar Nov 01 '25 18:11 schultzdavid

> try { }; say $!
(Any)

I'm not sure though it's specced that way, or if this value is more an accidental feature of the Rakudo implementation.

"specced" = roasted.

A search of roast for $! (plus some exploration of the results and blame) suggests that S02-magicals/dollar_bang.t is the pertinent part of the spec, and in particular this fragment:

try { die "foo" };
ok defined($!), 'error in try makes $! defined';
try { 1 };
nok $!.defined, 'successful try { } resets $!';

My interpretation of those tests is that an implementation is correct according to the spec (roast) if, after a try is complete, $!.defined is True if an exception was thrown during execution of the try and False otherwise.

raiph avatar Nov 02 '25 22:11 raiph

I believe PR 4733 addresses this issue in full.

arkiuat avatar Dec 16 '25 00:12 arkiuat