Back to Examples

Errors

Ballerina does not have exceptions. Instead functions report invalid states by returning error values. Each error value has,

  • 1.Message, a human-readable string value describing the error.
  • 2.Cause, which is an error value if this error was caused due to another error, which needs to be propagated, otherwise nil.
  • 3.Detail, a mapping value consisting of additional information about the error.
  • 4.Stack trace, a snapshot of the state of the execution stack when the error value was created.

Error values are immutable.

You can create a new error value using an error constructor. As the first argument to the error constructor, it expects the message string. As the second argument, you can optionally pass in an error? value for cause. Subsequent named arguments, if specified, will be used to create the detail mapping. The stack trace is provided by the runtime.

import ballerina/io;

type Person record {
    string name;
    int age;
};

function validatePeople(Person[] people) returns error? {
    if people.length() == 0 {
        // Create an error value specifying only the error message.
        return error("Expected a non-empty array");
    }

    foreach Person p in people {
        io:println("Validating ", p.name);
        error? err = validatePerson(p);
        if err is error {
            // Create a new error value with the validation error as the cause
            // and the `Person` value for which validation failed in the detail mapping.
            return error("Validation failed for a person", err, person = p);
        }
    }
}

function validatePerson(Person person) returns error? {
    int age = person.age;
    if age < 0 {
        // If validation fails for age, create a new error value specifying
        // an error message and the age value for which validation failed.
        return error("Age cannot be negative", age = age);
    }
}

public function main() {
    Person[] people = [
        {name: "Alice", age: 25},
        {name: "Bob", age: -1},
        {name: "Charlie", age: 30}
    ];
    // Note how the `Person` value after the value for which validation fails is
    // not processed.
    error? err = validatePeople(people);
    if err is error {
        printError(err);
    }
}

// Helper function to print internals of error value.
function printError(error err) {
    io:println("Message: ", err.message());
    io:println("Detail: ", err.detail());
    io:println("Stack trace: ", err.stackTrace());

    error? cause = err.cause();
    if cause is error {
        io:println("Cause:");
        printError(cause);
    }
}
$ bal run error_reporting.balValidating AliceValidating BobMessage: Validation failed for a personDetail: {"person":{"name":"Bob","age":-1}}Stack trace: [callableName: validatePeople  fileName: error_reporting.bal lineNumber: 20,callableName: main  fileName: error_reporting.bal lineNumber: 42]Cause:Message: Age cannot be negativeDetail: {"age":-1}Stack trace: [callableName: validatePerson  fileName: error_reporting.bal lineNumber: 30,callableName: validatePeople  fileName: error_reporting.bal lineNumber: 16,callableName: main  fileName: error_reporting.bal lineNumber: 42]

Related links

PreviousUnions
NextAnydata type