elves / elvish

Powerful scripting language & versatile interactive shell

Home Page:https://elv.sh/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

valleydali opened this issue · comments

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.

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

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.

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.