import ballerina/io;const ACCOUNT_NOT_FOUND = "AccountNotFound";
const INVALID_ACCOUNT_ID = "InvalidAccountID";function getAccountBalance(int accountID) returns int|error {
    if (accountID < 0) {
        return error(INVALID_ACCOUNT_ID, accountID = accountID);
    } else if (accountID > 100) {
        return error(ACCOUNT_NOT_FOUND, accountID = accountID);
    }
    return 600;
}public function main() {
    error simpleError = error("SimpleErrorType", message = "Simple error occurred");
    io:println("Error: ", simpleError.reason(),
                ", Message: ", simpleError.detail()?.message);    int|error result = getAccountBalance(-1);
    if (result is int) {
        io:println("Account Balance: ", result);
    } else {
        io:println("Error: ", result.reason(),
                    ", Account ID: ", result.detail()["accountID"]);
    }
}

Error Handling

Ballerina distinguishes ordinary errors such as failing to open a file or failing to send a network response, from an exceptional condition in the code, such as type cast errors. Error handling in Ballerina incorporates two different strategies depending on the type of error. For ordinary errors such as failing to open a file or failing to send a network response, the error value is returned. When the Ballerina runtime encounters exceptional conditions the runtime panics resulting in abrupt completion of execution, unless the error is explicitly handled.

Ballerina provides language constructs to handle both returned error values and panics. This example demonstrates how errors can be created, returned, and how returned errors can be handled.

Ballerina error values are immutable structured values composed of an error reason string, a stack trace representing the execution stack at the point at which the error value was created, and an error detail record.

import ballerina/io;
const ACCOUNT_NOT_FOUND = "AccountNotFound";
const INVALID_ACCOUNT_ID = "InvalidAccountID";
function getAccountBalance(int accountID) returns int|error {
    if (accountID < 0) {
        return error(INVALID_ACCOUNT_ID, accountID = accountID);
    } else if (accountID > 100) {

Return an error with “InvalidAccountID” as the reason if accountID is less than zero.

        return error(ACCOUNT_NOT_FOUND, accountID = accountID);
    }

Return an error with “AccountNotFound” as the reason if accountID is greater than hundred.

    return 600;
}

Return a value if accountID is in between zero and hundred inclusive.

public function main() {
    error simpleError = error("SimpleErrorType", message = "Simple error occurred");

Define an error of the generic error type using the default error constructor. The error reason must be the first and only positional argument to the default error constructor and the type of the value must be a subtype of string. Additional fields providing more details can be passed as named arguments to the constructor and the type of each of those must be a subtype of anydata|error.

    io:println("Error: ", simpleError.reason(),
                ", Message: ", simpleError.detail()?.message);

Print the error reason and the message field from the error detail. The .reason() and .detail() methods can be invoked on error values to retrieve the reason and details of the error. message is an optional field in the generic error Detail record.

    int|error result = getAccountBalance(-1);
    if (result is int) {
        io:println("Account Balance: ", result);

If the result is an int, then print the value.

    } else {
        io:println("Error: ", result.reason(),
                    ", Account ID: ", result.detail()["accountID"]);
    }
}

If an error is returned, print the reason and the account ID from the detail record. Each additional error detail field provided to the error constructor is available as a field in the error detail record.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
$ ballerina run error_handling.bal
Error: SimpleErrorType, Message: Simple error occurred
Error: InvalidAccountID, Account ID: -1