【发布时间】:2018-07-02 12:41:53
【问题描述】:
我目前正在制作一个返回对象的函数。但是如果我在输入中发现一些无效的东西,我想返回一些东西来表明这个问题。我只是无法解决这个问题..这里处理错误的正确方法是什么?使用选项?但是返回 None 并没有指定任何具体问题。我有什么问题吗?:)
case class myClass(value: Int)
def myFunction(input: Int) : myClass
{
//return myClass if succeded
//else return some error - what do I return here?
}
//I've read that to handle some error scenarios you should do something like this:
sealed trait SomeError
object SomeError{
case object UnknownValue extends SomeError
case object SomeOtherError extends SomeError
case object OutOfBounds extends SomeError
}
//But how do I return myClass and this at the same time in case of an error?
非常感谢!! ^^
【问题讨论】:
标签: scala error-handling options