<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 1. 捕獲異常 在`Kotlin`中異常的處理還是`try-catch-finally`,但是略微有所不同,比如下面的案例: ~~~ fun main() { try { var a = 3 / 0 } catch (e: Exception){ println(e.localizedMessage) } finally { } } ~~~ 結果: ``` / by zero ``` 上面的案例是相同的使用部分。對于`Kotlin`中對于異常的處理更加便捷,比如: ~~~ fun main() { var a = try { 3 / 0 } catch (e: Exception){ 0 } finally { Int.MAX_VALUE } println(a) } ~~~ 結果: ``` 0 ``` 從上面的代碼中我們可以體會到在`Kotlin`中處理異常的便捷。同時,注意到在`finally`中的代碼不會被`return`語句所影響。 # 2. 拋出異常 在`Java`中我們都知道需要在方法后面使用`throw`關鍵字進行異常拋出給調用者。在`Kotlin`中在方法后面不用聲明,使用同`Java`中的寫法,比如: ~~~ fun calc(a: Int, b: Int): Double{ if(b == 0) throw Exception("by Zero") return a * 1.0 / b } fun main() { var result = try { calc(3, 0) } catch (e: Exception){ println(e.localizedMessage) 0 } println(result) } ~~~ 結果: ``` by Zero 0 ``` # 3. 自定義異常 自定義的異常類必須繼承自`Throwable`類或其子類,比如下面的案例: ~~~ class MyException(override val message: String?): Throwable() { } fun calc(a: Int, b: Int): Double{ if(b == 0) throw MyException("by Zero") return a * 1.0 / b } fun main() { calc(3, 0) } ~~~ 結果: ``` Exception in thread "main" com.kotlinLearn.MyException: by Zero at com.kotlinLearn.KotlinDemoKt.calc(KotlinDemo.kt:11) at com.kotlinLearn.KotlinDemoKt.main(KotlinDemo.kt:16) at com.kotlinLearn.KotlinDemoKt.main(KotlinDemo.kt) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看