当前位置:网站首页>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
}
边栏推荐
- Do Netease and Baidu have their own tricks for seizing the beach AI learning machine?
- Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
- 【图像分类】2021-MLP-Mixer NIPS
- 静态博客搭建工具汇总
- Kotlin——函数
- Crowdfunding platform system based on JSP & Servlet
- pl/sql之各参数详解(“箱子模型“)
- 一名合格的软件测试工程师,应该具备哪些技术能力?
- XML file usage and parsing
- Leetcode 0140. word splitting II
猜你喜欢

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)

常用的弱网测试工具
![[image classification] 2021 MLP mixer nips](/img/fb/ca25da210c8da2b6b55f372a325a6c.png)
[image classification] 2021 MLP mixer nips

Appnium--APP自动化测试工具

Dynamic planning - 63. Different paths II

Day08 redis的基础知识

Adding DSP library to STM32F103 and its solution

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

ServletContext、request、response

Crowdfunding platform system based on JSP & Servlet
随机推荐
Un7.27: how to successfully build a ruoyi framework project in idea?
MySQL是怎么保证高可用的
Notes to subject 2
简单、好用的性能测试工具推荐
测试用例管理工具
过滤器、拦截器、监听器
LeetCode 0141. 环形链表 - 三种方法解决
Summary and interpretation of CONDA virtual environment
Detailed explanation of pointer written test questions (C language)
JDBC usage
程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...
Appnium--APP自动化测试工具
Remove screen cutting and copying restrictions
un7.27:如何在idea中成功搭建若依框架项目?
【无标题】
一名合格的软件测试工程师,应该具备哪些技术能力?
How does MySQL ensure high availability
C language: realize the exchange of two numbers without creating temporary variables
Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
Chinese Remainder Theorem of X problem