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 errors

Ballerina does not allow silently ignoring return values. To ignore a return value, assign it to _; this is like an implicitly declared variable that cannot be referenced. When a return type includes an error, you have to do something with the error. _ is of type any: you cannot use _ to ignore an error. checkpanic is like check, but panics on error rather than returning.

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)