当前位置: 首页 > news >正文

Kotlin异常处理runCatching,getOrNull,onFailure,onSuccess(1)

Kotlin异常处理runCatching,getOrNull,onFailure,onSuccess(1)

fun main(args: Array<String>) {var s1 = runCatching {1 / 1}.getOrNull()println(s1) //s1=1,打印1println("-")var s2 = runCatching {1 / 0 //发生异常}.getOrNull()println(s2) //s2为nullprintln("--")runCatching {1 / 1}.onFailure {println("onFailure $it") //不会执行,因为runCatching没有失败}.onSuccess {println("onSuccess $it") //执行,因为runCatching成功}println("---")runCatching {1 / 0 //发生异常}.onFailure {println("onFailure $it") //执行,因为runCatching失敗了}.onSuccess {println("onSuccess $it") //不会执行,因为runCatching沒有成功}
}

1
-
null
--
onSuccess 1
---
onFailure java.lang.ArithmeticException: / by zero

kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。https://blog.csdn.net/zhangphil/article/details/129800172

http://www.lryc.cn/news/180427.html

相关文章:

  • 【深入探究人工智能】:历史、应用、技术与未来
  • 【设计模式】五、原型模式
  • day36-注解
  • 【C语言数据结构——————栈和队列4000字详解】
  • 电子地图 | VINS-FUSION | 小觅相机D系列
  • C++goto语句
  • Spring学习笔记11 GoF代理模式
  • 代码随想录二刷 Day23
  • Ubuntu `apt` 报错 “Errors were encountered while processing: base-passwd“ 的解决方法
  • XXL-JOB分布式任务调度
  • 加拿大人工智能数据搜索平台【Secoda】完成1400万美元A轮融资
  • less与sass
  • c-const修饰指针-day16
  • 已解决: Go Error: no Go files in /path/to/directory问题
  • 2022年6月和7月的工作经历
  • 【图像处理】SIFT角点特征提取原理
  • flutter开发实战-应用更新apk下载、安装apk、启动应用实现
  • DispatcherServlet初始化之Spring容器创建1.0
  • CSS的基础
  • mathtype如何嵌入到word中?详细mathtype安装步骤教程
  • 云安全之访问控制的常见攻击及防御
  • Java编程技巧:跨域
  • react create-react-app 配置less
  • 树的表示——孩子兄弟表示法
  • Windows11安装MySQL8.1
  • Linux编程——经典链表list_head
  • 基于51单片机NEC协议红外遥控发送接收仿真设计( proteus仿真+程序+原理图+报告+讲解视频)
  • Jmeter分布式压力测试
  • Rust :mod.rs和lib.rs中use的作用
  • ISP图像信号处理——平场校正介绍以及C++实现