Scala 尝试捕获错误

Scala try and catch Error

所以这是我的代码:

def loadOperation(fileName:String): csvList = {
  try{
   val pattern = """^(.+);(\d{5});(4|2|31);(0|1);(.+);(\d+|\d+,\d+)$""".r
   Source.fromFile(fileName).getLines().foldLeft(List[CsvEntry]())((csvList, currentLine) =>
     currentLine match {
      case pattern(organisation,yearAndQuartal,medKF,trueOrFalse,name,money) => new CsvEntry(organisation,yearAndQuartal.toInt,medKF.toInt,trueOrFalse.toInt,name,money) :: csvList
      case default => csvList
      })
  } catch {
    case one: java.io.FileNotFoundException => println("This file could not be found!")
  }}

问题是我的代码不起作用,它总是显示以下错误:

type mismatch found: Unit required csvList which expands to List[CsvEntry]?

我该如何解决这个问题?

问题是 catch 子句:

println("This file could not be found!")

的类型是 Unit,显然不是 csvList。您应该添加一个额外的行 returns 一个空列表,例如

catch {
    case one: java.io.FileNotFoundException => println("This file could not be found!")
}

Nil