.\" Made by website/plain/build.mjs from website/words/try.md; change that file. .TH SG-TRY 7 2026-11-28 "SG" "SG reference" .SH NAME try \- opens a Result: its value, or the error handed to the caller .SH SYNOPSIS .TP \fBtry f()\fR the value of a Result, or a return of its error .TP \fBtry x\fR on a T?: the value, or a return of null .TP \fB(try f()).field\fR a try in the middle of a chain .TP \fBx ?? value\fR no try: the value, or this one in place of the error .TP \fBx ?? return ...\fR no try: the value, or a return written out .SH DESCRIPTION A function that can fail returns a \fBResult\fR, which is \fBOk(value)\fR or \fBError(cause)\fR\&. \fBtry\fR opens it: on \fBOk\fR it gives the value, and on \fBError\fR the function returns that error to its caller at once. It is an ordinary return: what the function owns is let go on the way out, and nothing is unwound. .PP The function with the \fBtry\fR must return a \fBResult\fR too. When its error type differs, \fBtry\fR puts the error into the one variant of that type that carries it. There is no conversion to write by hand; when no variant carries it, or two do, the compiler says so. .PP On a \fBT?\fR, in a function that returns a \fBT?\fR, \fBtry\fR gives the value or returns \fBnull\fR\&. .PP \fBtry\fR costs one test of the \fBResult\fR, as a C function that returns an error code costs one test of the code. .SH EXAMPLES .SS "Pass the error on" .RS 4 .nf Result sum(read string a, read string b) { var x = try int.parse(a) var y = try int.parse(b) return Ok(x + y) } void main() { print(sum("40", "2") ?? -1) match sum("40", "2x") { Ok(n) => print(n) Error(why) => print(why) } } .fi .RE .PP .RS 4 .nf 42 Invalid(1) .fi .RE .PP \fBint.parse("2x")\fR fails, so \fBsum\fR returns its error and never adds. \fBInvalid(1)\fR says the text stopped being a number at byte 1. .SS "An error of your own" .RS 4 .nf enum ConfigError : Formattable { BadNumber(NumberError cause) OutOfRange(int value) } Result parsePort(read string text) { var port = try int.parse(text) if port < 1 || port > 65535 { return Error(ConfigError.OutOfRange(port)) } return Ok(port) } void main() { for text in "8080 80x 99999".splitWhitespace() { match parsePort(text) { Ok(port) => print($"port {port}") Error(error) => print($"error: {error}") } } } .fi .RE .PP .RS 4 .nf port 8080 error: BadNumber(Invalid(2)) error: OutOfRange(99999) .fi .RE .PP \fBint.parse\fR fails with a \fBNumberError\fR, and \fBtry\fR returns it as \fBConfigError.BadNumber\fR, the one variant that carries a \fBNumberError\fR\&. .SS "On a value that may be null" .RS 4 .nf read string? secondWord(read string line) { var words = line.splitWhitespace() _ = try words.next() return try words.next() } void main() { print(secondWord("make it so") ?? "none") print(secondWord("alone") ?? "none") } .fi .RE .PP .RS 4 .nf it none .fi .RE .PP \fBwords.next()\fR gives \fBnull\fR when there are no more words, and \fBtry\fR then returns \fBnull\fR from \fBsecondWord\fR\&. .SS "In the middle of a line" .RS 4 .nf struct Point { int x int y } Result parsePoint(read string text) { var parts = text.split(',') var x = try int.parse(parts.next() ?? "") var y = try int.parse(parts.next() ?? "") return Ok(Point { x = x, y = y }) } Result sumOfX(read List texts) { var total = 0 for text in read texts { total += (try parsePoint(text)).x } return Ok(total) } void main() { var good = List { "1,2", "3,4" } var bad = List { "1,2", "x,4" } print(sumOfX(read good) ?? -1) print(sumOfX(read bad) ?? -1) } .fi .RE .PP .RS 4 .nf 4 -1 .fi .RE .PP \fBtry\fR binds to the whole call after it, so a field of what it opens is reached through brackets. .SS "From main" .RS 4 .nf Result port(read string text) { return int.parse(text) } Result main() { var a = try port("8080") print($"first: {a}") var b = try port("80x") print($"second: {b}") return Ok() } .fi .RE .PP .RS 4 .nf first: 8080 error: Invalid(2) .fi .RE .PP A \fBmain\fR that returns a \fBResult\fR may use \fBtry\fR too. An error it ends with is printed after \fBerror: \fR, and the program exits with code 1. .SH "WHAT THE COMPILER REFUSES" .SS "try in a function that cannot pass the error on" .RS 4 .nf Result parse() { return Ok(1) } int twice() { return 2 * try parse() } void main() { print(twice()) } .fi .RE .PP .RS 4 .nf error: `try` returns the error to the caller, but `twice` returns `int` --> example.sg:6:16 6 | return 2 * try parse() | ^^^^^^^^^^^ hint: make `twice` return a `Result<..., string>`, or handle the error here with `match` or `??` .fi .RE .SS "An error no variant carries" .RS 4 .nf enum AppError { Disk(int code) } Result load(read string text) { return Ok(try int.parse(text)) } void main() { print(load("1") ?? 0) } .fi .RE .PP .RS 4 .nf error: `try` cannot turn `NumberError` into `AppError` --> example.sg:6:15 6 | return Ok(try int.parse(text)) | ^^^^^^^^^^^^^^^^^^^ hint: add a variant to `AppError` that carries it: `Name(NumberError cause)` .fi .RE .SS "An error two variants carry" .RS 4 .nf enum AppError { First(NumberError cause) Second(NumberError cause) } Result load(read string text) { return Ok(try int.parse(text)) } void main() { print(load("1") ?? 0) } .fi .RE .PP .RS 4 .nf error: `try` cannot choose: `First` and `Second` of `AppError` both carry `NumberError` --> example.sg:7:15 7 | return Ok(try int.parse(text)) | ^^^^^^^^^^^^^^^^^^^ hint: open the `Result` with `match` and build the one you mean .fi .RE .SS "A Result left unopened" .RS 4 .nf Result save() { return Ok() } void main() { save() } .fi .RE .PP .RS 4 .nf error: this `Result` is not used, so an error would be lost silently --> example.sg:6:5 6 | save() | ^^^^^^ hint: pass the error on with `try save()`, or drop it on purpose with `_ = save()` .fi .RE .SH "SEE ALSO" .BR sg-match (7), .BR sg-is (7), .BR sg-null (7) .PP https://sg\-lang.org/reference/try.html