[Kotlin] Try catch & Throw
2023-09-14 09:00:46 时间
In java, it requires you to handle the exception when you declaring and using the code:
public class BoringJavaCode { public static Double divide(int a, int b) throws InterruptedException { Thread.sleep(1000); return (double)a / b; } public static void main(String args[]) { try { System.out.println(divide(6,3)); } catch (InterruptedException e) { e.printStackTrace(); } } }
but in kotlin, it is not requried. Kotlin has @Throws(), but it is only for Java developer to know that they need to handle that exception.
// for java to check the code @Throws(InterruptedException::class) fun main(args: Array<String>) { var result = try { // but kotlin doesn't require you mention throw divide(5, 23) } catch(e: Exception) { println(e) 0 // return value } println(result) }
fun divide (a: Int, b: Int): Double {
Thread.sleep(1000)
return (a.toDouble()) / b
}
相关文章
- 图像处理-激光测距技术&工业相机基本原理概述「建议收藏」
- pdca原则 & smart 原则
- jupyter notebook 之 matplotlib & seaborn
- 7 Papers & Radios | ECCV 2022最佳论文;Transformer在试错中自主改进
- 7 Papers & Radios | 用神经网络推开数学推理大门;世界首个宏基因组蛋白质图谱
- 【超详细】*和&在C/C++中的常见用法(附示例讲解)
- 【进阶指针二】数组传参&指针传参&函数指针&函数指针数组&回调函数
- layui怎么表格中显示图片&&layui为字段设置默认排序
- 数据存在CNGBdb,可以发哪些杂志?【Cell Death & Diseas】 | CNGBdb-Question Time
- 7 Papers & Radios | Meta「分割一切」AI模型;从T5到GPT-4盘点大语言模型
- <meta>正则
- url参数中有+、空格、=、%、&、#等特殊符号的问题解决
- php获得url参数中具有&的值的方法