Kotlin Nothing_抛出异常

或许你对他很陌生 那么看这样一段代码

Kotlin Nothing_android_02

指向TODO()

运行起来会怎么样呢

运行上述代码是没事的。

但是下面的代码就报错了

package com.anguomob.learn
const val number=1001;
fun main() {
when(number){
1-> TODO();
1001-> TODO();

}
}

报错内容

Exception in thread "main" kotlin.NotImplementedError: An operation is not implemented.
at com.anguomob.learn.LearnKTKt.main(LearnKT.kt:6)
at com.anguomob.learn.LearnKTKt.main(LearnKT.kt)

其实TODO就是一个Nothing类型

 

Kotlin Nothing_抛出异常_03

当然不仅仅todo

也有像 exitProcess

Kotlin Nothing_抛出异常_04

 他们都是要抛出异常来进行退出 所以就用到了这个