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

时间:2022-09-22 21:36:18

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

 
 
 
 

ballerina 学习十六 错误&&异常处理的更多相关文章

  1. 强化学习&lpar;十六&rpar; 深度确定性策略梯度&lpar;DDPG&rpar;

    在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...

  2. Python学习第十六篇——异常处理

    在实际中,很多时候时候,我们并不能保证我们所写的程序是完美的.比如我们程序的本意是:用户在输入框内输入数字,并进行后续数学运算,即使我们提醒了用户需要输入数字而不是文本,但是有时会无意或者恶意输入字符 ...

  3. python学习第十八天 --错误&amp&semi;异常处理

    这一章节主要讲解python的错误和异常处理 什么是错误和异常?及其区别? 错误: 1.语法错误:代码不符合解释器或者编译器语法. 2.逻辑错误:不完整或者不合法输入或者计算出现问题.   异常:执行 ...

  4. C&plus;&plus;(四十六) — 异常处理机制、标准IO输入输出

    1.异常处理机制 一般来说,异常处理就是在程序运行时对异常进行检测和控制.而在C++ 中,使用 try-throw-catch模式进行异常处理的机制. #include<iostream> ...

  5. Scala学习十六——XML处理

    一.本章要点 XML字面量<like>this</like>的类型为NodeSeq 可以在XML字面量中内嵌Scala代码 Node的child属性产出后代节点 Node的at ...

  6. Java学习十六

    学习内容: 1.做毕设 2.Java异常类 3.Java包装类 1.System.exit(1):终止程序运行,终止final执行方法 2.throws抛出异常类型,throw抛出异常对象 用法:th ...

  7. java web 学习十六(JSP指令)

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范*定义了三个指令: pa ...

  8. MYSQL数据库学习十六 安全性机制

    16.1 MYSQL数据库所提供的权限 16.1.1 系统表 mysql.user 1. 用户字段 Host:主机名: User:用户名: Password:密码. 2. 权限字段 以“_priv”字 ...

  9. Flask 学习 十六 部署

    部署流程 manage.py 部署命令 每次安装升级只需运行deploy命令即可完成操作 @manager.command def deploy(): """执行部署任务 ...

随机推荐

  1. WPF入门教程系列十五——WPF中的数据绑定&lpar;一&rpar;

    使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...

  2. Azure Application Gateway &lpar;2&rpar; 面向公网的Application Gateway

    <Windows Azure Platform 系列文章目录> 本章将介绍如何创建面向公网的Application Gateway,我们需要准备以下工作: 1.创建新的Azure Reso ...

  3. Invoke--转载

    在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法是错误的做法,Invoke 和 BeginInvoke 就是为了解决这个问题而出现的,使你在多线程中安全的更新界 ...

  4. WinForm窗体嵌入

    一.在winform窗体上添加两个控件 1.容器>Panel 2.添加 SideBar.dll (下载链接:http://pan.baidu.com/s/1o6qhf9w) (1)将SideBa ...

  5. Swift 自己主动引用计数机制ARC

    Swift 使用自己主动引用计数(ARC)这一机制来跟踪和管理你的应用程序的内存.通常情况下,Swift 的内存管理机制会一直起着作用,你无须自己来考虑内存的管理.ARC 会在类的实例不再被使用时,自 ...

  6. QT5&colon; QApplication&comma; no such file or directory

    In QT5, when I use this: #include<QApplication>, QT tells :no such file or directory, even tho ...

  7. 一览css布局标准

    回顾历史,CSS1于1996.12.17发正式版,它是为辅助HTML的展现效果而生的.1998.5.12,CSS2发正式版.随后发修订版CSS2.1,纠正了CSS2中的一些错误.注意从CSS2起,CS ...

  8. HTTP 错误 404&period;8 - Not Found

    HTTP 错误 404.8 - Not Found请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 详细错误信息模块 RequestFilteringModule 通 ...

  9. Hadoop的编译

    Hadoop2.4.0  重新编译 64  位本地库 原创作者:大鹏鸟 时间:2014-07-28 环境:虚拟机 VirtualBox,操作系统 64 位 CentOS 6.4 下载重新编译需要的软件 ...

  10. 折腾Java设计模式之中介者模式

    博文原址:折腾Java设计模式之中介者模式 中介者模式 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类通常处理不同类之间的通信,并 ...