Scala abnormal

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/chen18677338530/article/details/91451044

Scala abnormal Introduction

Here Insert Picture Description

object FunDemo8 {

  def main(args: Array[String]): Unit = {

    try {
      val r = 10 / 0
    } catch {
      case ex: ArithmeticException => {
        println(ex)
      }
      case ex: Exception => {
        println("捕获异常")
      }
    } finally {
      println("最后执行的代码")
    }

    println("--------")
  }
}

Here Insert Picture Description

Failure Notes

Here Insert Picture Description

object FunDemo9 {

  def main(args: Array[String]): Unit = {

    def test():Nothing = {
      throw new ArithmeticException()
    }

    test()
  }
}

Here Insert Picture Description

Here Insert Picture Description

object FunDemo10 {
  def main(args: Array[String]): Unit = {
    f1()
  }

  @throws(classOf[NumberFormatException])
  def f1() = {
    "abc".toInt
  }
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/chen18677338530/article/details/91451044