Throw Custom Exception

Viewed 58245

I'm trying to throw a custom exception.

The implementation of the custom exception class is:

case class customException(smth:String)  extends Exception

In my code I wrapped a piece of code that I'm sure throws throw an exception with try/catch to throw my customException.

try{
    val stateCapitals = Map(
      "Alabama" -> "Montgomery",
      "Alaska" -> "Juneau",
      "Wyoming" -> "Cheyenne")

    println("Alabama: " + stateCapitals.get("AlabamaA").get)
}
catch{
    case x:Exception=>throw classOf[CustomException]
}

I got a compilation error that says :

        found   : java.lang.Class[CustomException]
[INFO]  required:    java.lang.Throwable 
[INFO]       case x:Exception=>throw classOf[CustomException]

How could I throw my own custom exception on this case? Later I'm checking if the thrown exception is of a type[x] to do something specific.

2 Answers
Related