Example
error IOError { NotFound(path: string) }fn open(p: string): !int { ... }fn main(): int { immut n: int = try open("x"); // ERROR: 'main' returns 'int' return n;}// Hint: Handle the error here instead: `f() recover as err run { ... }`.try e passes the error of e on: if e (a !T) failed, try returns that
same error value – same variant, same payload, same message – from the
function it is written in, after running that function’s pending defers
exactly as a return would. On success it is just the value. So it needs:
- A function that returns
!V(anyV).mainreturnsint, so nothing can fail out of it: handle the error there withrecover. A function returning a plain type can be declared!Tto let errors leave it. - A place a
returnis allowed. Adeferruns while the function is already returning, so atryanywhere inside one is an error. Handle the error inside the deferred statement:defer f() recover run { ... };. - A fallible operand.
try plain()whereplainreturns aninthas no error to pass on; drop thetry. - A function. There is no function to return from at the top level.
try is a reserved word, and binds like a prefix operator: try a() + try b()
is (try a()) + (try b()).
Run turf --explain E095 in a terminal to read this again.