当前位置:网站首页>null安全与异常
null安全与异常
2022-07-28 04:05:00 【紫罗兰与海棠】
一、null安全
- Kotlin把运行时间可能会出现的null问题,以编译时错误的方式,提前在编译期,而不是等到运行时报错,防患于未然,提高了程序的健壮性。
- 可空性
在Kotlin中,除非另有规定,变量不可为null值,即默认变量不为空,不能给非空类型变量赋null值
- Kotlin不允许在可空类型值上调用函数,除非你主动接受安全管理

安全调用操作符
安全调用允许可空类型上调用函数,编译器看到有安全调用安全符,如果遇到null值,就跳过函数调用,而不是返回null
使用带let的安全调用
当输入不为空时,调用let函数
var letter = readLine()?.let {
if (it.isNotBlank()) {
it.capitalize()
} else {
"butterfly"
}
}
非空断言操作符 !!
当变量值为null时,会抛出空指针异常

空合并操作符 ?:
如果左边的求值为null,就使用右边的结果值
二、异常
自定义异常
class MyException : IllegalArgumentException("操作不当")
抛出异常
throw Exception("Hi There!")
异常处理
try {
// 一些代码
}
catch (e: SomeException) {
// 处理程序
}
finally {
// 可选的 finally 块
}
Nothing类型
在 Kotlin 中 throw 是表达式,所以你可以使用它(比如)作为 Elvis 表达式的一部分:
val s = person.name ?: throw IllegalArgumentException("Name required")throw 表达式的类型是特殊类型 Nothing。 该类型没有值,而是用于标记永远不能达到的代码位置。 在你自己的代码中,你可以使用 Nothing 来标记一个永远不会返回的函数:
fun fail(message: String): Nothing { throw IllegalArgumentException(message) }
先决条件函数
先决条件函数定义了先决条件,若条件满足,目标代码才能执行
fun check(letter : String?): String{
val s = checkNotNull(letter) {
"something 不好" }
return s
}
边栏推荐
- C language: find the number of 1 in binary stored in memory as an integer
- pl/sql之各参数详解(“箱子模型“)
- 40: Chapter 4: Development File Service: 1:fastdfs: (1): introduction to fastdfs;
- Day08 redis的基础知识
- Un7.27: how to successfully build a ruoyi framework project in idea?
- Notes to subject 2
- conda虚拟环境总结与解读
- Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
- Simple and easy-to-use performance testing tools recommended
- Combination of Oracle and Premier League statistics and presentation
猜你喜欢

cookie与Session

Implementation of online rental system based on SSM

超好用的 PC 端长截图工具

Data rich Computing: m.2 meets AI at the edge

程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...

H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame

conda虚拟环境总结与解读

It's amazing. This article completely explains the service interface testing

Machine learning 06: Decision Tree Learning

Machine learning 07: Bayesian learning
随机推荐
Notes to subject 2
企业数字化建设“三不五要”原则
金仓数据库KingbaseES安全指南--5.2. 数据完整性保护
离职前一定要做好这7件事情,少一件都很麻烦。
[prototype and prototype chain] get to know prototype and prototype chain~
Un7.27: how to successfully build a ruoyi framework project in idea?
Remove screen cutting and copying restrictions
Day08 redis的基础知识
Super easy to use PC end long screenshot tool
Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving
Iterator function operation of iterator learning
UBI read only file system
7/27(板子)染色法判定二分图+求组合数(递推公式)
Convert py file to exe executable file
C语言:求一个整数存储在内存中的二进制中1的个数
RT thread changes the print serial port (add other functions on the basis of BSP)
TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
Leetcode 0140. word splitting II
XML file usage and parsing
Istio's Traffic Management API