# Errors and branching by value: try / catch / throw / match
try / catch / throw, `match` as statement and as value, ternary, yes/no variables, `??`. Open when handling server errors or branching on many values.

## Errors and branching by value (`try` / `catch` / `throw` / `match`)
```js
try {
  var part = l.slice(1, 9) // out of range: server error index_out_of_bounds, the try body stops here
  give_reward(part)        // errors inside called functions are caught too (also after code::wait)
} catch (e) {             // runs only when an error happened; e is text
  log("failed: $e")       // server error → its id (expected_but_got, index_out_of_bounds, ...); throw → the message
}
// the code after try/catch always runs
try { risky() }                       // no catch: ignore the error
try { ... } catch { log("failed") }   // catch without a name
if (hp < 0) { throw("hp < 0: $hp") }  // throw("message") / throw "message": stops the line, try catches it

match (phase) {                       // x is evaluated once
  Phase.LOBBY -> start_lobby()        // one statement or { block }
  GAME, END -> { log("busy") }        // several values: one equals with a list; bare members work for an enum
  else -> log("?")                    // optional; must be last
}
match (value::health) { 1, 2 -> log("low")
  else -> log("ok") }                 // arms: new line, ; or , between them
```
- `try` compiles to `code::catch_error` (controller_exception) + a line var flag: ≈3 actions over the body (flag = 1,
  the block, flag = 0, one `>` check; checking e itself would cost ≈6–7); `try` alone with no exits is just the block.
  `return` in a function, `break` / `continue` of a loop and `return` / `break` that end an event (end_thread) work
  inside try as outside. Nested try, try in loops and recursion
  are correct. A `std::session` opened inside try is closed when an error skips its end. An empty `catch { }` is
  the same as no catch. `throw` = `code::throw_error("throw", message, "ERROR")`; the id is shown nowhere (e and the log get the
  message). `throw_error(..., "WARNING")` / `"FATAL"` only log, the code continues and catch does not see them.
- Not caught: `code::end_thread()` / `break` outside a loop inside a FUNCTION called from try exits only the try
  body and runs catch (e keeps its old value), the code after try goes on — warning `try-end-thread` at the call;
  return a flag from the function and `return` / `break` in the try body itself. No `finally` (the code after the block runs anyway), no types `catch (e: T)`.
- `match` = `if (x == a) { } else if (x == [b, c]) ...` exactly as hand-written; with `-O` a single-number arm becomes
  number_in_range (≈1.5 instead of ≈8, x must be a number variable). Errors: duplicate values, `=>` (use `->`),
  `_` / `default` (use `else`), a list `[1, 2]` (write `1, 2`), conditions (use if). On an enum without `else` the
  compiler warns which members have no arm. No `switch` / `when` / `case`.
- `match` as a statement runs actions; for a value use `var x = match (...) { 1 -> "one", else -> "?" }` (see
  «Conditions as values» below). A bare value in a statement arm (`1 -> "one"` with nothing assigned) is an error. A bare value as a statement elsewhere
  (`"a"`, `5`, `x`) does nothing: warning `bare-value`.
- `try`, `catch`, `finally`, `throw`, `match` are keywords (not variable names); inside `( )` they stay argument names
  (`variable::purge(["a"], match = "STARTS_WITH")`).

## Conditions as values
Same JSON as the hand-written if / else, no hidden cost:
```js
var kit = vip ? "gold" : "iron"                  // if (vip) { kit = "gold" } else { kit = "iron" }
player::message(coins >= price ? "&aOK" : "&cNo") // as an argument / in text: a temp + the same if / else
var label = match (state) { State.LOBBY -> "Lobby", State.GAME -> "Game", else -> "?" }
var ok = hp > 5 && lvl < 3                        // 1 or 0: ok = 0; if (...) { ok = 1 }
if (ok) { ... }   if (!ok && x > 1) { ... }      // yes/no variable: ok > 0 (≈1)
var price = prices[id] ?? 10                      // missing key → 10: the same read action, 0 extra
var name = nick ?? "guest"                        // variable not set → "guest"
```
- `c ? a : b` (nested: `a ? 1 : b ? 2 : 3`): branches are lazy (a function call in a branch runs only there).
  `x = c ? a : x` is just `if (c) { x = a }`. A constant condition (`inline var DEBUG = 0`) picks the branch at
  compile time; `if (DEBUG)` too. A ternary alone on a line is an error (`ternary-statement`): write if / else.
- `match` as a value: one value per arm, `else ->` required unless the arms cover the whole enum
  (`match-expression-else`).
- `if (ok)` only for a yes/no variable: everything written to it is a comparison, `true` / `false`, 0 / 1, a yes/no
  function or another yes/no variable; or it is declared `var ok: bool` / `function f(on: bool)`. `true` / `false`
  written to such a variable become 1 / 0. Text in `if (name)` is error `bool-text`: compare explicitly
  (`if (name != "")`). `"${a == b}"` prints 1 / 0.
- `??` works on a variable and on `m[k]` / `l[i]` / `m.get(k)`; after a function call or an expression it is an error
  (`coalesce-target`).
`a < b < c` is a syntax error — write `a < b && b < c`. `x ??= f()` calls f only when x is not set.
