LoginSignup
4
4

More than 5 years have passed since last update.

Scalaで例外を定義

Posted at

Javaとの互換性のために補助コンストラクタを追加。

class MyException(message: String = null, cause: Throwable = null) extends RuntimeException(message, cause) {
  def this(cause: Throwable) = this(Option(cause).map(_.toString).getOrElse(null), cause)
}

こんな感じで使える。

new MyException()
new MyException("msg")
new MyException(new Exception("hoge"))
new MyException("addtional message", new Exception("hoge"))

Scalaならもちろん引数名を指定しても良い。

new MyException(cause = new Exception("hoge"))
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4