Custom Exception in scala

前端 未结 7 883
你的背包
你的背包 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:48

    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")
    }
    

提交回复
热议问题