2

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

I'd like to write an Scala exception class which sub-classes the Java Exception class, and provides similar constructors (each of which invokes the corresponding Exception ctor. I.e. how would I write the Scala equivalent of this Java class:

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 appears to be related to this question however the answers therein aren't satisfactory (in that a sub-class isn't actually defined).

Community
  • 1
  • 1
jon hanson
  • 8,722
  • 2
  • 37
  • 61
  • The accepted answer to that question doesn't answer my question in that it doesn't call different constructors. It instead cheats by using knowledge of the implementation details of Exception and calls the same base class constructor for each subclass constructor. – jon hanson Jun 13 '12 at 09:57
  • 2
    In Scala it is not possible to have multiple ctors. So the accepted answer of the linked question does answer your question. – kiritsuku Jun 13 '12 at 10:12
  • It's as @Antoras said: http://scala-programming-language.1934581.n4.nabble.com/how-to-call-super-class-s-contructor-td1934801.html – tenshi Jun 13 '12 at 10:15
  • @Antoras Scala does have multiple constructors - http://daily-scala.blogspot.co.uk/2009/11/multiple-constructors.html – jon hanson Jun 13 '12 at 10:18
  • 1
    @jon-hanson but if you read carefully: [...] In Java if a constructor calls another constructor that call must be the first statement in the constructor. Scala is the same except that in Scala the primary constructor must be called. [...] So you should always call *primary constructor* from the alternative one and you can't call `super(...)`. – tenshi Jun 13 '12 at 10:22
  • @tenshi. Right, so Scala allows multiple constructors but only a single primary constructor. – jon hanson Jun 13 '12 at 10:24
  • To rephrase the above question, what's the idiomatic way of achieving this (without making assumptions about the internals of the base class)? – jon hanson Jun 13 '12 at 10:26
  • @jon-hanson yes, exactly. And this primary constructor will call super-class constructor – tenshi Jun 13 '12 at 10:26
  • @jon-hanson How would you make no assumption about the base class if you call `super`? Once again, it is totally similar to the question linked above. – Nicolas Jun 13 '12 at 11:46
  • Note that you can't subclass without making assumptions about the base class. Pick a book like Effective Java, and you'll see tons of recommendations that are all related to the fact that subclasses cannot avoid making assumptions. – Daniel C. Sobral Jun 13 '12 at 13:50

2 Answers2

7

How about using default values (null)?

class YourException (msg:String=null, cause:Throwable=null)
  extends java.lang.Exception (msg, cause) {}
user unknown
  • 35,537
  • 11
  • 75
  • 121
1

This case of overloaded constructors each calling a superclass constructor is the only case I know of where Scala cannot produce equivalent byte code to Java. In Scala, the primary constructor must be called. Secondary constructors can be defined, but they can only call the primary constructor.

I think the Scala way is much better, but it does mean the precise equivalent of your exception class cannot be defined. This has never caused me a problem in practice, as you can pass null or some other default to the most general superclass constructor.

Lachlan
  • 3,744
  • 2
  • 26
  • 29