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

posted on   荣锋亮  阅读(215)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-05-19 jQuery操作表格(table)的常用方法、技巧汇总

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示