Custom Exception in scala

前端 未结 7 880
你的背包
你的背包 2020-12-24 12:02

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 :

7条回答
  •  孤独总比滥情好
    2020-12-24 12:43

    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
    }
    

提交回复
热议问题