Scala

Scala

Made by DeepSource

Exception caught is generic SC-W1005

Bug risk
Major

Suppressing exceptions is generally a bad idea and defeats the purpose of exception handling. Each type of exception provides an insight into what exactly went wrong and provides scenario-specific ways for graceful recovery. While it is easy to recover from some exceptions, a small subset of them make the recovery very difficult, usually because the conditions are not suitable for the program to continue executing. It is therefore suggested that you switch to a better approach of exception handling and recovery.

Bad Practice

try {
  // do something
} catch {
  case ex: Exception => ...
}

Recommended

try {
  // do something
} catch {
  case fooEx: FooException => ...
  case barEx: BarException => ...
  case _ => ...
}

A more idiomatic approach would be -

Try(/* do something */) match {
  case Success(...) => ...
  case Failure(e: FooException) => ...
  case Failure(e: BarException) => ...
  case Failure(e) => ...
}

Make sure to import Try, Success and Failure from scala.util.

References:

https://docs.scala-lang.org/overviews/scala-book/functional-error-handling.html