Java中异常分两种, 受检异常和 非受检异常
受检异常:在编程时 程序员 编辑好每一种可能异常的处理方法, 确定代码冗余
非受检异常: 在编程时 程序员 不用设计好 每种异常处理方法, 减少代码冗余
Scala中 处理异常 跟Java一致
try{} catch{}
eg:
try{ val reader = new FileReader("input.txt") }catch { case fileNotFoundException: FileNotFoundException => {println("this is a file not find ex")} case ex: IOException => {println("this is a IOexception")} }finally { println("there is a error") }
输出: