当前位置:网站首页>Android kotlin exception handling
Android kotlin exception handling
2022-06-23 17:47:00 【Susceptible to cold】
Throws Exception
Kotlin Exceptions and Java The same as , try…catch…finally Code block handling exception , The only difference is :Kotlin All the exceptions are Unchecked exceptions.
checked exceptions Is an exception that must be defined and handled on a method , such as Java Of IoException.
Kotlin Throw an exception using Throws Annotation to achieve , as follows :
@Throws(IOException::class)
fun createDirectory(file: File) {
if (file.exists())
throw IOException("Directory already exists")
file.createNewFile()
}When we're in java When used in code , as follows :
Will remind us to report errors , But if kotlin When used in the document , There will be no hint .
Custom exception
/** * @author : zhaoyanjun * @time : 2021/7/5 * @desc : Custom exception */ class CommonException(code: Int, message: String) : Exception(message)
Use :
@Throws(CommonException::class)
fun createDirectory(file: File) {
if (file.exists())
throw CommonException(0, "file is exists")
file.createNewFile()
}runCatching
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
kotlin.runCatching {
sum(2, 4)
}.onSuccess {
Log.d("yy--", " Normal results $it")
}.onFailure {
it.printStackTrace()
Log.d("yy--", " The result is abnormal ${it.message}")
}
}
fun sum(num1: Int, num2: Int): Int {
1 / 0
return num1 + num2
}
}getOrDefault
val result = kotlin.runCatching {
sum(2, 4)
}.onSuccess {
Log.d("yy--", " Normal results $it")
}.onFailure {
it.printStackTrace()
Log.d("yy--", " The result is abnormal ${it.message}")
}.getOrDefault(100)
// If it works properly , Just go back to 6, Return if the operation is abnormal 100
Log.d("yy--", " result $result")isSuccess、isFailure
val result = kotlin.runCatching {
sum(2, 4)
}.onSuccess {
Log.d("yy--", " Normal results $it")
}.onFailure {
it.printStackTrace()
Log.d("yy--", " The result is abnormal ${it.message}")
}.isFailureexceptionOrNull
public fun exceptionOrNull(): Throwable? =
when (value) {
is Failure -> value.exception
else -> null
}If there is an exception, it returns an exception , Otherwise return to Null
getOrNull
@InlineOnly
public inline fun getOrNull(): T? =
when {
isFailure -> null
else -> value as T
}If it fails , Just go back to null , Otherwise return to
边栏推荐
- Discussion on five kinds of zero crossing detection circuit
- Cross browser common events
- Postgresql_ Optimize SQL based on execution plan
- 开户券商怎么选择?现在网上开户安全么?
- FPN characteristic pyramid network
- How to use SQL window functions
- Analytic analog-to-digital (a/d) converter
- 一文入门智能开关的3种功能形态
- QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]
- MySQL - reasons for using repeatable read
猜你喜欢

How to use SQL window functions

qYKVEtqdDg
![QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]](/img/62/a0425a325f123ba91a7a69cf854d2c.png)
QT layout manager [qvboxlayout, qhboxlayout, qgridlayout]

FPN characteristic pyramid network

Troubleshooting of datanode entering stale status

Easyplayer mobile terminal plays webrtc protocol for a long time. Pressing the play page cannot close the "about us" page

qYKVEtqdDg

DataNode进入Stale状态问题排查
NLP paper reading | improving semantic representation of intention recognition: isotropic regularization method in supervised pre training

The official Chinese course of zero foundation introduction jetpack compose is coming
随机推荐
[JS reverse hundred examples] pedata encryption information and zlib Application of gunzipsync()
How long does it take to open a stock account by mobile phone? Is online account opening safe?
How to make sales management more efficient?
MySQL transaction submission process
What can the accelerated implementation of digital economy bring to SMEs?
Cross browser common events
[go] calling Alipay to scan code for payment in a sandbox environment
解答02:Smith圓為什麼能“上感下容 左串右並”?
History of storage technology: from tape to hardware liquefaction
C. Phoenix and Towers-Codeforces Global Round 14
A number of individual stocks in Hong Kong stocks performed actively, triggering investors' speculation and concern about the recovery of the Hong Kong stock market
Goframe framework: fast implementation of service end flow limiting Middleware
微信小程序:酒店预计到店日期的时间选择器
DataNode进入Stale状态问题排查
开户券商怎么选择?现在网上开户安全么?
浅析3种电池容量监测方案
High availability solution practice of mongodb advanced applications (4)
[network communication -- webrtc] source code analysis of webrtc -- bandwidth estimation at the receiving end
Freemark uses FTL files to generate word
美团三面:聊聊你理解的Redis主从复制原理?