Scala

Scala

Made by DeepSource

Exception variable is declared but unused SC-W1082

Bug risk
Major

The catch clause allows you to trap Exceptions by allowing you to specify cases via the case keyword. This allows you to decide error handling and recovery depending on the type of Exception caught. However, not using the declared exception variable makes it redundant. Either do not declare the exception variable at all, or, use it appropriately such as when logging errors.

Bad Practice

try {
  // ...
} catch {
  case e: IllegalArgumentException =>
    // Error handling
}

Recommended

// Scenario 1: Do not declare an exception variable
try {
  // ...
} catch {
  case _: IllegalArgumentException =>
    // Error handling
}

// Scenario 2: Use the exception variable
try {
  // ...
} catch {
  case e: IllegalArgumentException =>
    logger.log(Level.ERROR, e.getMessage)
}