Possible Duplicate:
define your own exceptions with overloaded constructors in scala




我想编写一个Scala异常类,该类继承Java Exception类的子类,并提供类似的构造函数(每个构造函数都调用相应的Exception ctor。即,我将如何编写与该Java类等效的Scala:

public class MyException extends java.lang.Exception {
    public MyException () {
    }

    public MyException (String msg) {
        super(msg);
    }

    public MyException (Throwable e) {
        super(e);
    }

    public MyException (String msg, Throwable cause) {
        super(msg, cause);
    }
}


这似乎与this question有关,但是其中的答案并不令人满意(因为实际上未定义子类)。

最佳答案

如何使用默认值(空)?

class YourException (msg:String=null, cause:Throwable=null)
  extends java.lang.Exception (msg, cause) {}

关于java - 如何在Scala中对异常进行子类化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11011926/

10-14 13:31