Kotlin 异常处理
来源:互联网 发布:淘宝达人怎么注销 编辑:程序博客网 时间:2024/06/01 10:18
Kotlin 的异常和 Java 的一样, try…catch…finally代码块处理异常,唯一一点不同是:Kotlin 的异常都是 Unchecked exceptions。
checked exceptions 是必须在方法上定义并且处理的异常,比如 Java 的 IoException。
Unchecked exceptions 不是必须处理的,比如 NullPointerException。
Kotlin 的异常这么设计,估计是尝试修正 Java 上异常没有达到理论效果。
Kotlin 异常的使用和 Java 一样:
val input = Files.newInputStream(path)try { var byte = input.read() //} catch (e: IOException) { // logcat} finally { input.close()}
方法采用注解的方式抛出异常。
@Throws(IOException::class)fun createDirectory(file: File) { if (file.exists()) throw IOException("Directory already exists") file.createNewFile()}
参考
《Programming Kotlin》Stephen Samuel ,Stefan Bocutiu
《Kotlin in Action》Dmitry Jemerov,Svetlana Isakova
阅读全文
0 0
- Kotlin 异常处理
- Kotlin 中 TODO 抛异常怎么处理?
- Kotlin-异常
- Kotlin-异常
- Kotlin之Anko异常处理(PluginManager$StartupAbortedException)
- Kotlin基础教程-异常
- Kotlin-33.异常(Exception)
- kotlin 中的异常
- Kotlin-null的处理
- kotlin 图片浮雕处理
- Step into Kotlin - 06 - 异常
- Kotlin笔记(九)异常
- Kotlin的空指针处理
- 异常处理+异常+处理异常+自定义异常
- 使用kotlin写个异常小框架
- 【Kotlin】官方文档翻译-异常(Exceptions)
- Kotlin 第十七章: 异常和注解
- view的空指针异常(Kotlin)
- Android源码--网络--libnetutils
- Windows 下开发常用快捷键小结
- Error:Cannot change dependencies of configuration ':app:_debugAnnotationProcessor' after it has been
- IDEA配置maven
- Kotlin学习(3)
- Kotlin 异常处理
- Python ——tkinter应用之时钟
- iis发布问题之Framework 版本
- AS中Git与GitHub的使用入门
- YourSQLDba备份失败案例锦集
- win10使用进阶:一周年更新14316如何开启Linux Bash命令行
- jsp/servlet中链接中出现中文乱码问题的解决办法及个人见解
- 收藏的URL
- nyoj 34 韩信点兵