elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.53k stars 297 forks source link

try {...} else {...} is documented but doesn't work #1734

Closed valleydali closed 3 months ago

valleydali commented 7 months ago

The language specification (https://elv.sh/ref/language.html#try), section 8.9 (Exception control: Try), item 3, states:

  1. If no exception occurs and else is present, else-block is executed. Example:
~> try { nop } else { echo well }
well

But this does not work. I get:

% try { nop } else { echo well }
Compilation error: try must be followed by a catch block or a finally block
  [tty 8]:1:1: try { nop } else { echo well }

Unfortunate, because this would be a useful feature if it worked.

I am using the latest release, 0.19.2.

krader1961 commented 7 months ago

The documentation needs clarification. A catch or finally block is always required. Try

try { nop } catch { nop } else { echo well }

Also, the "If catch is present" item is somewhat incorrect as catch or finally is always required:

elvish> try { nop }
Compilation error: try must be followed by a catch block or a finally block
  [tty 53]:1:1: try { nop }
elvish> try { nop } finally { put finally }
▶ finally
valleydali commented 7 months ago

OK, thanks! Yes, try-catch-else works.

Interestingly, try-finally-else does not work... but try-else-finally does. I guess finally really wants to be last, which makes sense.

krader1961 commented 6 months ago

Whether try-finally-else should work is debatable. Personally, I am opposed to allowing the clauses appear in every possible ordering. Why not finally-try-else? As long as the documentation is clear I have no objection to imposing an order on the clauses.