当前位置:网站首页>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
}
边栏推荐
- 离职前一定要做好这7件事情,少一件都很麻烦。
- Super easy to use PC end long screenshot tool
- 金仓数据库KingbaseES安全指南--6.1. 强身份验证简介
- Leetcode 0140. word splitting II
- jdbc使用
- Un7.27: how to successfully build a ruoyi framework project in idea?
- 常用的接口测试工具
- 金仓数据库KingbaseES安全指南--4 数据访问保护
- Kingbasees Security Guide for Jincang database -- 4 data access protection
- Summary of static blog building tools
猜你喜欢

Fourier series
![[leetcode] 34. Find the first and last positions of elements in the sorted array](/img/f0/3eaa33fa7b13abe5f27b136239507d.png)
[leetcode] 34. Find the first and last positions of elements in the sorted array

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

How does MySQL ensure high availability

Program life | test engineers only know a little? Seven shortcuts teach you to learn new technology quickly

Machine learning 07: Bayesian learning
![[prototype and prototype chain] get to know prototype and prototype chain~](/img/8a/d6362fdd50dc883ff817a997ab9e1e.png)
[prototype and prototype chain] get to know prototype and prototype chain~

Adding DSP library to STM32F103 and its solution

Day08 redis的基础知识

WordPress simple mkblog blog theme template v2.1
随机推荐
Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
Do Netease and Baidu have their own tricks for seizing the beach AI learning machine?
Dynamic programming - 474. One and zero
Go结构体
C#跨线程刷新前台UI
Program life | test engineers only know a little? Seven shortcuts teach you to learn new technology quickly
Summary of static blog building tools
常用的接口测试工具
What technical capabilities should a qualified software testing engineer have?
Adding DSP library to STM32F103 and its solution
过滤器、拦截器、监听器
7/27(板子)染色法判定二分图+求组合数(递推公式)
C language: find the number of 1 in binary stored in memory as an integer
Fourier series
Detailed explanation of pointer written test questions (C language)
[reach out to Party welfare] the easiest way to scan the H5 page in wechat
Leetcode58. Length of the last word
21天,胖哥亲自带你玩转OAuth2
Greed 122. The best time to buy and sell stocks II
Greed 45. Jumping game II