Swan Lake Beta3 is here.
import ballerina/io;

public function main() returns error? {
    do {
        // If either `foo()` or `bar()` invocations returns an `error`,
        // the error will be returned from the `main` function and execution
        // of the `main` function ends.
        check foo();
        check bar();

        if !isOK() {
            // Fails explicitly with an `error`.
            fail error("not OK");

        }
    }
    // Failure with the respective error is caught by the `on fail` block.
    on fail var e {
        io:println(e.toString());
        return e;
    }

}

function foo() returns error? {
    io:println("OK");
}

function bar() returns error? {
    io:println("OK");
}

function isOK() returns boolean {
    // Returns `false`.
    return false;

}

Check Semantics

check semantics is not simply to return on error. When check gets an error, it fails. Enclosing block decide how to handle failure. Most blocks pass failure up to enclosing block. Function definition handles failure by returning the error. on fail can catch the error. fail statement is like check but always fails. Differs from exceptions in that control flow is explicit

import ballerina/io;
public function main() returns error? {
    do {
        check foo();
        check bar();

If either foo() or bar() invocations returns an error, the error will be returned from the main function and execution of the main function ends.

        if !isOK() {
            fail error("not OK");

Fails explicitly with an error.

        }
    }
    on fail var e {
        io:println(e.toString());
        return e;
    }

Failure with the respective error is caught by the on fail block.

}
function foo() returns error? {
    io:println("OK");
}
function bar() returns error? {
    io:println("OK");
}
function isOK() returns boolean {
    return false;

Returns false.

}
bal run check_semantics.bal
OK
OK
error("not OK")
error: not OK {}
import ballerina/io;

public function main() returns error? {
    do {
        // If either `foo()` or `bar()` invocations returns an `error`,
        // the error will be returned from the `main` function and execution
        // of the `main` function ends.
        check foo();
        check bar();

        if !isOK() {
            // Fails explicitly with an `error`.
            fail error("not OK");

        }
    }
    // Failure with the respective error is caught by the `on fail` block.
    on fail var e {
        io:println(e.toString());
        return e;
    }

}

function foo() returns error? {
    io:println("OK");
}

function bar() returns error? {
    io:println("OK");
}

function isOK() returns boolean {
    // Returns `false`.
    return false;

}

Check Semantics

check semantics is not simply to return on error. When check gets an error, it fails. Enclosing block decide how to handle failure. Most blocks pass failure up to enclosing block. Function definition handles failure by returning the error. on fail can catch the error. fail statement is like check but always fails. Differs from exceptions in that control flow is explicit

import ballerina/io;
public function main() returns error? {
    do {
        check foo();
        check bar();

If either foo() or bar() invocations returns an error, the error will be returned from the main function and execution of the main function ends.

        if !isOK() {
            fail error("not OK");

Fails explicitly with an error.

        }
    }
    on fail var e {
        io:println(e.toString());
        return e;
    }

Failure with the respective error is caught by the on fail block.

}
function foo() returns error? {
    io:println("OK");
}
function bar() returns error? {
    io:println("OK");
}
function isOK() returns boolean {
    return false;

Returns false.

}
bal run check_semantics.bal
OK
OK
error("not OK")
error: not OK {}
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand