scala 调用超级构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6726740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:13:34  来源:igfitidea点击:

Call Super Constructor

scala

提问by Echo

I have a custom exception class like the following :

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

case class CustomException(errorMsg:String)  extends Exception(error:String)

All what I need that when I catch exception is to throw my custom exception and pass my error message to the custom exception . I expect from CustomException constructor to call super(errMsg) However , this isn't what goes now and I got a compilation error .

当我捕获异常时,我所需要的只是抛出我的自定义异常并将我的错误消息传递给自定义异常。我希望从 CustomException 构造函数调用 super(errMsg) 但是,这不是现在发生的事情,我收到了编译错误。

 catch {
      case s: Exception => throw CustomException("This is a custom message")
    }

How could I call the super constructor :

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

super(errorMessage)

回答by Kim Stebel

case class CustomException(errorMsg:String)  extends Exception(errorMsg)

回答by dhg

case class CustomException(errorMsg:String)  extends Exception(errorMsg)

You're calling the superclass's constructor, but the argument you are passing (error) isn't bound to anything.

您正在调用超类的构造函数,但您传递的参数 ( error) 未绑定到任何内容。