云星数据---Scala实战系列(精品版)】:Scala入门教程055-Scala实战源码-Scala 异常处理

来源:互联网 发布:体检报告生成软件 编辑:程序博客网 时间:2024/06/14 18:08

Scala 异常处理

package scala_learn.demo13_Exceptionimport java.io.{FileNotFoundException, FileReader, IOException}/** * Created by liguohua on 2017/3/31. *//** * 1.在scala中的异常和java中的异常处理模型基本一致 * 2.对应异常的匹配方式与java不大一样 * 3.scala中的异常没有checkedException,相当于scala中全是runtimeException */class O1_ExceptionDemo {}object O1_ExceptionDemo {  def main(args: Array[String]) {    test2  }  //演示try-catch-finally  def test2: Unit = {    try {      val f = new FileReader("input.txt")    } catch {      case ex: FileNotFoundException => {        println("Missing file exception"+ex.getMessage)        //throw  ex//并不影响finally的执行,但会影响异常块后的执行      }      case ex: IOException => {        println("IO Exception"+ex.getStackTrace)      }    } finally {      println("Exiting finally...")    }    println("here is coming.....")  }  //演示try-catch  def test1: Unit = {    try {      val f = new FileReader("input.txt")    } catch {      case ex: FileNotFoundException => {        println("Missing file exception" + ex)        ex.printStackTrace()      }      case ex: IOException => {        println("IO Exception")        //抛出异常        throw ex      }    }    println("here is coming....")  }}
阅读全文
0 0
原创粉丝点击