[英]How to return None in Scala
For my first Scala program, I am trying to write a small utility which will convert an XML file from one schema to another. 对于我的第一个Scala程序,我正在尝试编写一个小工具,它将XML文件从一个模式转换为另一个模式。
I started writing a method which will give me the file contents: 我开始编写一个方法,它将为我提供文件内容:
def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
try
{
val source = scala.io.Source.fromFile(filename, encoding)
val contents = source.mkString
source.close()
return Some(contents)
}
catch
{
return None
}
}
But it does not compile. 但它没有编译。 I get back "value apply is not a member of Nothing" and "value isDefinedAt is not a member of Nothing" as error messages from the line
return None
. 我回来了“值apply不是Nothing的成员”和“value isDefinedAt不是Nothing的成员”,因为该行的错误消息
return None
。
All the examples I can find returning an Option use matching, but this does not make sense here. 我可以找到返回选项使用匹配的所有示例,但这在这里没有意义。 I just want to not fail if I can't read the file for some reason.
如果由于某种原因我无法读取文件,我只想不失败。
What should I do in this case? 在这种情况下我该怎么办? Is there a pattern for doing this kind of thing in Scala?
在Scala中有这种做事的模式吗?
In this specific case (exception handling), I'd suggest using a Try instead. 在这种特殊情况下(异常处理),我建议使用Try代替。
def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
Try {
val source = scala.io.Source.fromFile(filename, encoding)
val contents = source.mkString
source.close()
return Some(contents)
}.toOption
}
I would, however, recommend against catching the exception at all. 但是,我会建议不要删除异常。 You're swallowing the cause for the error by returning
None
: is it a FileNotFoundException
? 你通过返回
None
来吞下错误的原因:它是FileNotFoundException
吗? a standard IOException
? 标准的
IOException
? was there a message with the error ( Unsupported encoding
comes to mind…)? 是否有错误消息(想到
Unsupported encoding
...)?
My rule of thumb is to let the caller deal with exceptions. 我的经验法则是让调用者处理异常。 If he doesn't care about the error itself, handling things is as easy as:
如果他不关心错误本身,处理事情就像:
Try {loadFile("test.txt")}.toOption
Better yet, since Try
has all the required methods, it can be used in a for-comprehension in a rather neat way: 更好的是,由于
Try
具有所有必需的方法,因此可以以相当简洁的方式用于理解:
for(s <- Try {loadFile("test.txt")};
i <- Try {s.toInt}) yield i
This will result in either a Success[Int]
or a Failure
that contains an exception describing exactly what went wrong. 这将导致
Success[Int]
或Failure
包含一个描述确切错误的异常。
There is all about "catch"
. 所有关于
"catch"
。
In scala it's supposed to be like this to make it compile: 在scala中,它应该像这样使它编译:
def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
try {
val source = scala.io.Source.fromFile(filename, encoding)
val contents = source.mkString
source.close()
Some(contents)
} catch {
case x: IOException => None
case x => errorHandler(x) // if any other exception
}
}
def errorHandler(e: Any) = None // put some logic here..
So use: 所以使用:
catch { case: x:ExceptionType ={ .. handling .. }}
In Scala catch
is a function that accepts another function as argument. 在Scala中,
catch
是一个接受另一个函数作为参数的函数。 So having what you have would complain about apply function. 因此,拥有你所拥有的东西会抱怨应用功能。
case
provides that function (PartialFunction) that catch
wants. case
提供了catch
函数(PartialFunction)。 (in nutshell) (简而言之)
Note: All exceptions are unchecked
in Scala
even IOException
. 注意:
Scala
甚至IOException
所有异常unchecked
。
Try this: 尝试这个:
def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
try
{
val source = scala.io.Source.fromFile(filename, encoding)
val contents = source.mkString
source.close()
return Some(contents)
}
catch
{
case e:Exception=>{
return None
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.