ballerina 学习十五 错误&&异常处理

ballerina 的error 处理和elxiir 以及rust 比较类似使用模式匹配,但是他的 error lifting 还是比较方便的
同时check 也挺好,异常处理没什么特殊的 throw 以及 throw catch finally

简单例子

  • error-handling
import ballerina/io;
function getAccountBalance(int accountID) returns (int|error) {

    if (accountID < 100) {
        error err = { message: "Account with ID: " + accountID + 
                      " is not found" };
        return err;
    } else {
        return 600;
    }
}

function main(string… args) {
    var r = getAccountBalance(24);

    match r {
        int blnc => {
            io:println(blnc);
        }
        error err => {
            io:println("Error occurred: " + err.message);
        }
    }
}
  • error lifting
import ballerina/io;
type Response {
    Status|error status;
};
type Status {
    string message;
    int code;
};
function main(string… args) {
    error e = { message: "response error" };
    Response|error firstResponse = e;
    int|error statusCode1 = firstResponse!status!code;
    io:println("The status code: ", statusCode1);

    Response|error|() secondResponse = ();

    int|error|() statusCode2 = secondResponse!status!code;
    io:println("The status code: ", statusCode2);
}
  • check
import ballerina/io;
type Person {
    string name;
    Address? address;
};
type Address {
    string street;
    string city;
};
function getAddress(Person p) returns (Address|error) {
    match (p.address) {
        Address add => { return add;}
        () => {
            error addNotFoundErr = { message: "address not found" };
            return addNotFoundErr;
        }
    }
}

function validateAddress(Person person) returns (boolean|error) {
    string city = check getAddress(person)!city;

    io:println(person.name, " has a valid city");
    return true;
}

function validateAddressAgain(Person person) returns boolean {
    string city = check getAddress(person)!city;

    io:println(person.name, " has a valid city");
    return true;
}

function main(string… args) {
    Person bob = { name: "bob" };
    Address address = { street: "1st Avenue", city: "Manhattan" };
    bob.address = address;
    io:println("validating bob…");
    var bobResult1 = validateAddress(bob);
    io:println("Bob's result 1:", bobResult1);
    boolean bobResult2 = validateAddressAgain(bob);
    io:println("Bob's result 2:", bobResult2);
    Person tom = { name: "tom" };
    io:println("\n", "validating tom…");
    var tomResult1 = validateAddress(tom);
    io:println("Tom's result 1:", tomResult1);
    var tomResult2 = validateAddressAgain(tom);
    io:println("Tom's result 2:", tomResult2);
}

  • throw
import ballerina/io;
type Record {
    int id;
    string name;
};
function readRecord(Record|() value) {
    match value {
        Record rec => {
            io:println("Record ID: ", rec.id, ", value: ", rec.name);
        }
        (any|()) => {
            error err = { message: "Record is null" };
            throw err;
        }
    }
}

function main(string… args) {
    Record r1 = { id: 1, name: "record1" };
    readRecord(r1);
    Record|() r2;

    match r2 {
        Record rec => {
            io:println("Record: " + rec.name);
        }
        (any|()) => {
            readRecord(r2);
        }
    }

    Record r3 = { id: 3, name: "record3" };
    readRecord(r3);
}
  • thorw/catch/finally
import ballerina/log;
import ballerina/runtime;
import ballerina/io;
function main(string… args) {
    int result;
    try {
        io:println("Start dividing numbers");

        result = divideNumbers(1, 0);

    } catch (error err) {
        io:println("Error occurred: ", err.message);
    } finally {
        io:println("Finally block executed");
    }
}

function divideNumbers(int a, int b) returns int {
    return a / b;
}

参考资料

https://ballerina.io/learn/by-example/try-catch-finally.html
https://ballerina.io/learn/by-example/throw.html
https://ballerina.io/learn/by-example/check.html
https://ballerina.io/learn/by-example/error-lifting.html
https://ballerina.io/learn/by-example/error-handling.html

猜你喜欢

转载自www.cnblogs.com/rongfengliang/p/9062030.html
今日推荐