当前位置:网站首页>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
}
边栏推荐
- My creation anniversary
- [735. Planetary collision]
- 7/27 (board) dyeing method to determine bipartite graph + find combination number (recursive formula)
- Selenium--WEB自动化测试工具
- jdbc使用
- What technical capabilities should a qualified software testing engineer have?
- Leetcode58. Length of the last word
- RT-Thread改变打印串口(在BSP的基础上添加其他功能)
- Analysis of static broadcast transmission process
- UBI read only file system
猜你喜欢

Dynamic planning - 62. Different paths

企业数字化建设“三不五要”原则

Notes on writing questions in sword finger offer

Crowdfunding platform system based on JSP & Servlet

仿真测试断开服务器公网连接

Linux - MySQL advanced (day19)

jdbc使用

In the official online CV2 document, check the optional values of OpenCV specific parameters

【图像分类】2021-MLP-Mixer NIPS

Summary and interpretation of CONDA virtual environment
随机推荐
Ffmpeg common instructions
Iterator function operation of iterator learning
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
Un7.27: common commands of redis database.
C语言:不创建临时变量实现两数交换
Summary and interpretation of CONDA virtual environment
pl/sql之各参数详解(“箱子模型“)
Monotonic stack - 739. Daily temperature
[untitled]
[untitled]
Day08 redis的基础知识
常用的接口测试工具
【图像分类】2021-MLP-Mixer NIPS
Machine learning 06: Decision Tree Learning
un7.27:redis数据库常用命令。
LeetCode 0141. 环形链表 - 三种方法解决
Selenium--WEB自动化测试工具
ServletContext、request、response
[untitled]
Notes on writing questions in sword finger offer