I am writing the function that can catch exceptions of the certain type only.
def myFunc[A <: Exception]() {
try {
println("Hello world") // or something else
} catch {
case a: A => // warning: abstract type pattern A is unchecked since it is eliminated by erasure
}
}
What is the corrent way to bypass jvm type erasure in such case?
You could use
ClassTag
like in this answer.But I'd prefer this approach:
Usage:
Using
ClassTag
:Note also that in general you should use
Try
withrecover
method:Try
will catch onlyNonFatal
exceptions.