1. try {
    2. var n = 10 / 0
    3. } catch {
    4. case ex: ArithmeticException => println("发生算数异常")
    5. case ex : NullPointerException => println("发生异常1")
    6. case ex: Exception => ex.printStackTrace()
    7. } finally {
    8. println("释放资源 ")
    9. }
    1. Scala没有编译时异常,异常都是在运行的时候捕获处理
    2. throw 抛出异常对象,都是Throwable子类,throw表达式有类型,就是Nothing

      1. def test(): Nothing = {
      2. throw new Exception("不对")
      3. //底下的代码不会执行
      4. }
    3. Scala throws注解