import ballerina/io;

public function main() {
    // Allowed only if return value is ().
    doX();

    // Allowed if return value does not include 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!");
    }
}

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 return value is ().

    _ = getX();

Allowed if return value does not include 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!");
    }
}
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:27)
           ignoring_return_values_and_errors:main(ignoring_return_values_and_errors.bal:12)