Swan Lake Beta3 is here.
import ballerina/io;

// Converts bytes to a string and then to an int.
function intFromBytes(byte[] bytes) returns int|error {

    string|error ret = string:fromBytes(bytes);

    // The `is` operator can be used to distinguish errors
    // from other values.
    if ret is error {

        return ret;
    } else {
        return int:fromString(ret);
    }
}

// The `main` function can return an error.
public function main() returns error? {

    int|error res = intFromBytes([104, 101, 108, 108, 111]);    
    if res is error {
        // The `check` expression is shorthand for this pattern of
        // checking if a value is an error and returning that value.
        return res;

    } else {
        io:println("result: ", res);
    }
}

Error Handling

Usually, a function handles errors by passing them up to its caller. The main function can also return an error.

import ballerina/io;
function intFromBytes(byte[] bytes) returns int|error {

Converts bytes to a string and then to an int.

    string|error ret = string:fromBytes(bytes);
    if ret is error {

The is operator can be used to distinguish errors from other values.

        return ret;
    } else {
        return int:fromString(ret);
    }
}
public function main() returns error? {

The main function can return an error.

    int|error res = intFromBytes([104, 101, 108, 108, 111]);    
    if res is error {
        return res;

The check expression is shorthand for this pattern of checking if a value is an error and returning that value.

    } else {
        io:println("result: ", res);
    }
}
bal run error_handling.bal
error: {ballerina/lang.int}NumberParsingError {"message":"'string' value 'hello' cannot be converted to 'int'"}
import ballerina/io;

// Converts bytes to a string and then to an int.
function intFromBytes(byte[] bytes) returns int|error {

    string|error ret = string:fromBytes(bytes);

    // The `is` operator can be used to distinguish errors
    // from other values.
    if ret is error {

        return ret;
    } else {
        return int:fromString(ret);
    }
}

// The `main` function can return an error.
public function main() returns error? {

    int|error res = intFromBytes([104, 101, 108, 108, 111]);    
    if res is error {
        // The `check` expression is shorthand for this pattern of
        // checking if a value is an error and returning that value.
        return res;

    } else {
        io:println("result: ", res);
    }
}

Error Handling

Usually, a function handles errors by passing them up to its caller. The main function can also return an error.

import ballerina/io;
function intFromBytes(byte[] bytes) returns int|error {

Converts bytes to a string and then to an int.

    string|error ret = string:fromBytes(bytes);
    if ret is error {

The is operator can be used to distinguish errors from other values.

        return ret;
    } else {
        return int:fromString(ret);
    }
}
public function main() returns error? {

The main function can return an error.

    int|error res = intFromBytes([104, 101, 108, 108, 111]);    
    if res is error {
        return res;

The check expression is shorthand for this pattern of checking if a value is an error and returning that value.

    } else {
        io:println("result: ", res);
    }
}
bal run error_handling.bal
error: {ballerina/lang.int}NumberParsingError {"message":"'string' value 'hello' cannot be converted to 'int'"}
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