彩笺传翠墨 2009-08-27
51CTO编辑推荐:Scala编程语言专题
Scala的构造方法非常灵活(具体怎么个灵活法这里不多说了),但随之而来的是重载构造方法的麻烦。
Scala的构造方法:重载的麻烦
例如定义新异常类型。一般来说,自己定义的异常都属于checked异常,大都从Exception继承过来,所以也大都需要定义多个构造方法。如果用Java来定义,没什么好说的,重载就行,但是用Scala的话就有点麻烦。Scala规定所有重载的构造方法都必须调用或间接调用默认构造方法,所以必须使用如下的方法。
MyException(message: String, cause: Throwable) extends Exception(message, cause) {
def this(message: String): = this(message, null)
def this(cause: Throwable): = this(null, cause)
def this: = this(null, null)
} 当然,这样是可以工作的,但是仔细看看Throwable的实现就会发现如果传入的cause为null话会导致异常栈的丢失。而且最恶心的是Throwable没有提供相应的setter/getter,我们能做的就是调用构造方法。
所以我就想出了下面的怪招。
object SpcException {
def apply(message: String, cause: Throwable): Exception =
(new SpcException1(message, cause)).asInstanceOf[Exception]
def apply(message: String): Exception =
(new SpcException2(message)).asInstanceOf[Exception]
def apply(cause: Throwable): Exception =
(new SpcException3(cause)).asInstanceOf[Exception]
def apply(): Exception =
(new SpcException4).asInstanceOf[Exception]
}
trait SpcException
class SpcException1(message: String, cause: Throwable)
extends Exception(message, cause) with SpcException
class SpcException2(message: String)
extends Exception(message) with SpcException
class SpcException3(cause: Throwable)
extends Exception(cause) with SpcException
class SpcException4 extends Exception with SpcException