<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                **1. 捕獲異常** ```scala try{}catch{ case ex:Exception => {}}finally{} ``` ```scala def main(args: Array[String]): Unit = { try { val y = 100 val x = y / 0 println(y) } catch { case ex:ArithmeticException => { ex.printStackTrace() // java.lang.ArithmeticException: / by zero } case ex:IOException => { ex.printStackTrace() } case ex:Exception => { ex.printStackTrace() } } finally { println("Exiting finally...") // Exiting finally... } } ``` <br/> **2. 拋出異常** ```scala throw new Exception ``` ```scala def main(args: Array[String]): Unit = { val x = 3 if (x > 2) throw new Exception("3大于2") // Exception in thread "main" java.lang.Exception: 3大于2 } ``` <br/> **3. Either[A, B]處理異常** * Either[A, B] 表示要么包含一個類型為A的實例,要么包括一個類型為B的實例; * Either只有兩個子類型:Left、Right,如果Either[A, B]對象包含的是A的實例,則它是Left實例,否則是Right實例; * Either用于異常處理時,一般約定:Left 代表出錯的情況,Right 代表成功的情況; ```scala object App{ def main(args: Array[String]): Unit = { def divide(x:Int):Either[String, Int] = { if (x == 0) Left("除數不能為0") else Right(100/x) } def matchTest(x:Int)=divide(x) match { case Left(errMsg) => println(errMsg) case Right(result) => println(result) } matchTest(0) // 除數不能為0 matchTest(2) // 50 } } ``` <br/> **4. allCatch處理異常** ```scala object App{ def main(args: Array[String]): Unit = { scala.util.control.Exception.allCatch.opt("42".toInt) // Some(42) scala.util.control.Exception.allCatch.opt("42a".toInt) // None scala.util.control.Exception.allCatch.toTry("42".toInt) // 42 scala.util.control.Exception.allCatch.toTry("42a".toInt) // Failure (e) scala.util.control.Exception.allCatch.withTry("42".toInt) // Success(42) scala.util.control.Exception.allCatch.withTry("42a".toInt) // Failure (e) scala.util.control.Exception.allCatch.either("42".toInt) // Right(42) scala.util.control.Exception.allCatch.either("42a".toInt) // Left(e) } } ``` <br/> **5. failAsValue處理異常** 當發生異常時,會提供一個默認值。 ```scala object App{ def main(args: Array[String]): Unit = { val result = scala.util.control.Exception.failAsValue(classOf[Exception])(-9999)("42a".toInt) println(result) // -9999 } } ```
                  <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>

                              哎呀哎呀视频在线观看