天天看點

scala基礎:異常處理

scala異常處理

def test():Nothing = {
 throw new Exception("不對")
}      
def main(args: Array[String]): Unit = {
 f11()
}
@throws(classOf[NumberFormatException])
def f11()={
 "abc".toInt
}      

代碼示例

package zxl

object Test01_Exception {
  def main(args: Array[String]): Unit = {
    try{
      val n = 10 / 0
    } catch {
      case e: ArithmeticException => {
        println("發生算術異常")
      }
      case e: Exception => {
        println("發生一般異常")
      }
    } finally {
      println("處理結束")
    }
  }
}