import ballerina/io;
public function main() {
// Allowed only if the return value is `()`.
doX();
// Allowed if the return value does not include an `error`.
_ = getX();
// Use `checkpanic` if you don't want to handle an `error`.
checkpanic tryX(true);
checkpanic tryX(false);
}
function doX() {
io:println("Do X");
}
function getX() returns boolean {
io:println("Get X");
return true;
}
function tryX(boolean x) returns error? {
io:println("Try X");
if !x {
return error("error!");
}
return;
}
Ignoring return values and errorsBallerina does not allow silently ignoring return values.
To ignore a return value, assign it to |
import ballerina/io;
public function main() {
doX();
Allowed only if the return value is ()
.
_ = getX();
Allowed if the return value does not include an error
.
checkpanic tryX(true);
checkpanic tryX(false);
Use checkpanic
if you don’t want to handle an error
.
}
function doX() {
io:println("Do X");
}
function getX() returns boolean {
io:println("Get X");
return true;
}
function tryX(boolean x) returns error? {
io:println("Try X");
if !x {
return error("error!");
}
return;
}
bal run ignoring_return_values_and_errors.bal
Do X
Get X
Try X
Try X
error: error!
at ignoring_return_values_and_errors:tryX(ignoring_return_values_and_errors.bal:28)
ignoring_return_values_and_errors:main(ignoring_return_values_and_errors.bal:12)