首页  

Scala 异常     所属分类 scala 浏览量 616
scala 没有检查异常  
throw new Exception("error")
在catch块中进行模式匹配
try {
    file.write(stuff)
} catch{
    case e:java.io.IOException => // handle IO Exception
    case n:NullPointerException => // handle null pointer
}

try{
   Integer.parseInt("xxx")
}catch{
   case _ => 0
}


class AIsTooBig extends Exception {} class BIsTooBig extends Exception {} object ExceptionDemo { def main(args: Array[String]) { println(divideTest(6, 3)) println(divideTest(1, 0)) println(divideTest(200, 5)) println(divideTest(50, 25)) } def divideTest(a: Int, b: Int): Double = { try { return divide(a, b) } catch { case e: AIsTooBig => -1 case e: BIsTooBig => -2 case e: ArithmeticException => -3 case _ => 0 } } def divide(a: Int, b: Int): Int = { if (a > 100) throw new AIsTooBig if (b > 10) throw new BIsTooBig return a / b } }

上一篇     下一篇
Scala 元组

Scala 函数式编程

scala Any AnyRef Object AnyVal

scala 函数

scala case class

How is Akka used in Play