Skip to content

Instantly share code, notes, and snippets.

@MaryamZi
Last active September 28, 2019 09:25
Show Gist options
  • Save MaryamZi/3600982b589ec1c17a5f39006077ee15 to your computer and use it in GitHub Desktop.
Save MaryamZi/3600982b589ec1c17a5f39006077ee15 to your computer and use it in GitHub Desktop.
import ballerina/io;
public function main() {
// If `foo()` does not panic, x will hold
// the value returned by `foo()`
int|string|error x = foo(1);
io:println(x);
// If `foo()` panics, x will hold
// the error associated with the panic.
x = trap foo(3);
io:println(x);
io:println("Done");
}
function foo(int i) returns int|string {
match i {
1 => {
return 1;
}
2 => {
return "two";
}
_ => {
panic error("not one or two");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment