致电Super Constructor

Ech*_*cho 20 scala

我有一个自定义异常类,如下所示:

case class CustomException(errorMsg:String)  extends Exception(error:String)
Run Code Online (Sandbox Code Playgroud)

当我捕获异常时,我需要的是抛出我的自定义异常并将我的错误消息传递给自定义异常.我期望从CustomException构造函数调用super(errMsg)但是,这不是现在的情况,我得到了一个编译错误.

 catch {
      case s: Exception => throw CustomException("This is a custom message")
    }
Run Code Online (Sandbox Code Playgroud)

我怎么能调用超级构造函数:

super(errorMessage)
Run Code Online (Sandbox Code Playgroud)

Kim*_*bel 35

case class CustomException(errorMsg:String)  extends Exception(errorMsg)
Run Code Online (Sandbox Code Playgroud)

  • @Echo:因为它不是Java,并且因为超级构造函数只能*从类的主构造函数中调用,即便如此 - 它只能被调用为构造函数中的第一个操作.Scala的语法更好地代表了这种潜在的限制(由JVM强加) (16认同)

dhg*_*dhg 7

case class CustomException(errorMsg:String)  extends Exception(errorMsg)
Run Code Online (Sandbox Code Playgroud)

你正在调用超类的构造函数,但是你传递的参数(error)没有绑定任何东西.