catch clause matching Throwable or the wildcard pattern
ID |
scala.correctness.dangerous_catch_all |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Error Handling |
Language |
Scala |
Description
Reports a catch clause whose pattern is case _ ⇒ or matches Throwable, so it also catches the fatal errors the JVM raises when the process can no longer continue.
Rationale
Throwable covers OutOfMemoryError, StackOverflowError, LinkageError and thread interruption. Swallowing those keeps a process running on a heap it cannot allocate from or a class path it cannot resolve, so the failure surfaces later somewhere unrelated and the original cause is gone. It also hides scala.util.control.ControlThrowable, which Scala uses to implement non-local returns and Breaks, silently changing control flow.
The following code illustrates the pattern detected by this rule:
class Store {
def save(row: String): Unit = ()
def log(e: Throwable): Unit = ()
}
class Importer(store: Store) {
def importAll(rows: List[String]): Int = {
try {
rows.foreach(store.save)
rows.size
} catch {
// FLAGGED: catch clause matching Throwable or the wildcard pattern
case _: Throwable => 0
}
}
}