当前位置:网站首页>Null security and exception
Null security and exception
2022-07-28 04:07:00 【Violet and begonia】
One 、null Security
- Kotlin Put the running time that may appear null problem , In the wrong way at compile time , In advance at compile time , Instead of waiting for an error to occur , Nip in the bud , Improved program robustness .
- Emptiness
stay Kotlin in , Unless otherwise specified , Variable cannot be null null value , That is, the default variable is not empty , Non empty type variables cannot be assigned null value
- Kotlin Calling functions on nullable type values... Is not allowed , Unless you actively accept safety management

Security call operators
Safe calls allow functions to be called on nullable types , The compiler sees a security call security symbol , If you encounter null value , Just skip function calls , Instead of going back null
use let Security call for
When the input is not empty , call let function
var letter = readLine()?.let {
if (it.isNotBlank()) {
it.capitalize()
} else {
"butterfly"
}
}
Non empty assertion operators !!
When the value of the variable is null when , A null pointer exception will be thrown 

Null merge operator ?:
If the left evaluates to null, Just use the result value on the right 
Two 、 abnormal
Custom exception
class MyException : IllegalArgumentException(" Improper operation ")
Throw an exception
throw Exception("Hi There!")
exception handling
try {
// Some codes
}
catch (e: SomeException) {
// The handler
}
finally {
// Optional finally block
}
Nothing type
stay Kotlin in throw Is an expression , So you can use it ( such as ) As Elvis Part of an expression :
val s = person.name ?: throw IllegalArgumentException("Name required")throw The type of expression is a special type Nothing. The type has no value , It's used to mark code locations that can never be reached . In your own code , You can use Nothing To mark a function that will never return :
fun fail(message: String): Nothing { throw IllegalArgumentException(message) }
Prerequisite function
The prerequisite function defines the prerequisite , If the conditions are met , Object code can only be executed 
fun check(letter : String?): String{
val s = checkNotNull(letter) {
"something Not good. " }
return s
}
边栏推荐
- Greed - 55. Jumping game
- 巧用栈回溯,帮你快速定位问题
- .net upload files through boundary
- 程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...
- 【无标题】
- jdbc使用
- [untitled]
- un7.27:redis数据库常用命令。
- Kingbasees Security Guide for Jincang database -- 5.2. data integrity protection
- Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
猜你喜欢

CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing

Detailed explanation of pl/sql parameters ("box model")

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

Dynamic planning - 62. Different paths

Detailed explanation of pointer written test questions (C language)

常用的接口测试工具

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

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

一文读懂Plato Farm的ePLATO,以及其高溢价缘由

Iterator function operation of iterator learning
随机推荐
un7.27:redis数据库常用命令。
C语言:求一个整数存储在内存中的二进制中1的个数
Developing rc522 module based on c8t6 chip to realize breathing lamp
Summary of static blog building tools
《剑指offer》| 刷题小记
Detailed explanation of pl/sql parameters ("box model")
[untitled]
Do Netease and Baidu have their own tricks for seizing the beach AI learning machine?
Day08 redis的基础知识
ftp服务器、nfs服务器的搭建和使用
【图像分类】2021-MLP-Mixer NIPS
Appnium -- app automated test tool
Several ways of connecting upper computer and MES
Greed 45. Jumping game II
仿真测试断开服务器公网连接
@Requiredargsconstructor annotation
[day03] process control statement
虚拟机类加载机制
Detailed explanation of pointer written test questions (C language)
RT-Thread改变打印串口(在BSP的基础上添加其他功能)