61

how can i create custom exceptions in Scala extending Exception class and throw them when exception occurs as well as catch them.

example in java :

class CustomException extends Exception {

  public final static String _FAIL_TO_INSERT = "FAIL_TO_INSERT";

}

7 Answers 7

71
final case class CustomException(private val message: String = "", 
                           private val cause: Throwable = None.orNull)
                      extends Exception(message, cause) 

Just try catch:

try {
    throw CustomException("optional")
} catch {
    case c: CustomException =>
          c.printStackTrace
}
3
  • 5
    I would actually avoid using a case-class. toString is already implemented, while equals will be overridden and will behave differently than other exceptions (Exception uses the default Object.equals). Also, public access to the message and cause is already available via getMessage and getCause. If you want pattern matching, implement the unapply method.
    – Eyal Roth
    May 12, 2017 at 15:46
  • Exceptions are not designed to be key in map, why do you want to compare exception?. Method toString returns the same result as for Exception (did you test it?). I will change visibility of message and cause to private to hide details. May 15, 2017 at 7:48
  • @Andrez I never said I want to compare Exceptions. tbh the equals behavior is probably the least important difference. Yes, toString behaves exactly like Exception.toString -- this is exactly what I said -- and that is one less reason to use a case class. Having the case class values private just makes it totally redundant. There's also the issue with the constructors which I've been discussing in my full answer.
    – Eyal Roth
    May 23, 2017 at 14:42
40
class MyException(message: String) extends Exception(message) {

  def this(message: String, cause: Throwable) {
    this(message)
    initCause(cause)
  }

  def this(cause: Throwable) {
    this(Option(cause).map(_.toString).orNull, cause)
  }

  def this() {
    this(null: String)
  }
}

This is almost identical to @Jacek L.'s answer. I just wanted to add some more input on the motive behind this answer.

Why so many constructors?

Throwable is written in kind of a funny way. It has 4 constructors -- ignoring the one with the boolean toggles -- each of them behaves a bit differently with nulls, and these differences could only be maintained with multiple constructors.

It would have been a bit cleaner if Scala would have allowed to call a superclass constructor via super, but it doesn't :(

Why not a case class?

  • Perfectly maintaining the constructors' behavior regarding nulls wouldn't be possible; specifically, both def this() and def this(message: String) will have to set the cause to null, while originally it is set to this.
  • toString will not be overridden.
  • The message and the cause are already publicly available via getMessage and getCause. Adding another reference to these is redundant.
  • equals will be overridden and will behave differently.
    Meaning, new Exception("m") == new Exception("m") // false
    while new CaseException("m") == new CaseException("m") // true

If one desires to access the message and the cause via pattern-matching, one can simply implement the unapply method:

object MyException {
  def unapply(e: MyException): Option[(String,Throwable)] = Some((e.getMessage, e.getCause))
}
2
  • 1
    can you please explain the this(null: String) part? thanks Apr 30, 2018 at 14:26
  • 6
    @NickGinanto In MyException class there are two constructors with a single parameter - the main one with message: String and the one with cause: Throwable. The compiler wouldn't be able to infer the invocation to this(null) since it is applicable to both of these constructors. Specifying the type of the null tells the compiler which constructor to call.
    – Eyal Roth
    Apr 30, 2018 at 15:12
11

You might want to create a sealed trait:

sealed trait MyException {
  // This is called a "self annotation". You can use "self" or "dog" or whatever you want.
  // It requires that those who extend this trait must also extend Throwable, or a subclass of it.
  self: Throwable =>
  val message: String
  val details: JsValue
}

Then you can have as many case classes as you need extending not only Exception, but your new trait.

case class CustomException(message: String) extends Exception(message) with MyException {
  override val details: JsValue = Json.obj("message" -> message, "etc" -> "Anything else")
}

Now, the whole point of using Scala is walking towards a more functional programming style, it will make your app more concurrent, so if you need to use your new custom exception, you might want to try something like this:

  def myExampleMethod(s: Option[String]): Future[Boolean] = Try {
    s match {
      case Some(text) =>
        text.length compareTo 5 match {
          case 1 => true
          case _ => false
        }
      case _ => throw CustomException("Was expecting some text")
    }
  }
  match {
    case Success(bool) => Future.successful(bool)
    case Failure(e) => Future.failed(e)
  }
2
  • 2
    It doesn't have to be sealed, but it helps the compiler if you need to pattern match the exceptions. I like this answer because the trait doesn't extend per se the Exception, but requires those who use the trait, to extend it. The advantages of this is that, because scala requires you to extend using a specific constructor, with this solution you are not married to just one of the many constructors an exception has.
    – caeus
    Jul 7, 2016 at 16:40
  • I think there's a typo with an unnecessary : in val:. I cannot edit the question as apparently Suggested edit queue is full. Jun 29, 2017 at 9:33
9

In order to reflect all the original constructors from Exception I'd implement a custom exception with the following pattern:

class CustomException(msg: String) extends Exception(msg) {
  def this(msg: String, cause: Throwable) = {
    this(msg)
    initCause(cause)
  }

  def this(cause: Throwable) = {
    this(Option(cause).map(_.toString).orNull)
    initCause(cause)
  }

  def this() = {
    this(null: String)
  }
}

This can be also achieved with a trait as mentioned in previous answer. I'd just not create individual classes in this case:

trait SomeException { self: Throwable =>
  def someDetail: SomeDetail
}

then, when throwing:

throw new Exception(...) with SomeException {
  override val someDetail = ...
}

and when matching:

try {
  ...
} catch {
  case ex: Throwable with SomeException =>
    ex.getCause
    ex.getMessage
    ex.someDetail
}

The advantage here is that you are not sticking to any particular constructor of the parent exception.

something more or less like that.

0
6

You define your custom exception like this

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

And you can throw your exception like this:

try{
...
} catch{
case x:Exception => throw new CustomException("whatever")
}
0
3

Similar to other answers, but I prefer to use a companion object instead of alternate constructors.

class MyException(message: String, cause: Throwable = null) extends Exception(message, cause)

object MyException {
  def apply(message: String): MyException = new MyException(message)
  def apply(message: String, cause: Throwable): MyException = new MyException(message, cause)
}
2

Adding to all the answers above, If at all you want to have an error hierarchy, abstract class would help.

abstract class GenericError(message: String) extends Exception(message)

case class SpecificErrorA(message: String) extends GenericError(message)

case class SpecificErrorB(message: String) extends GenericError(message)


throw new SpecificErrorA("error on A") // OR
throw new SpecificErrorB("error on B")

The same is possible using a trait instead of an abstract class, but they are limited in that they do not have constructor parameters.

Probably use GenericError everywhere and deconstruct (pattern match) it on the application/controller boundary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.