A catch clause should not just rethrow the exception

  • USELESS_CATCH
  • Code Quality
  • Low
  • No tags

This rule applies when a catch clause just rethrows the caught exception.

The catch clause is useless if the action taken is just rethrowing the same exception. Therefore, it is recommended to remove the catch clause or add comments (e.g., TODO et al.) for understanding the code.

Noncompliant Code Example

View with compliant examples side by side
// USELESS_CATCH alarm at the 'catch' clause because it only rethrows the caught exception.
try {
    riskyFunction();
} catch (e) {
    throw e;
}

Compliant Code Example

View with noncompliant examples side by side
// Remove alarm by adding comments.
try {
    riskyFunction();
} catch (e) {
    // riskyFunction can fail when ...
    throw e;
}

Version

This rule was introduced in DeepScan 1.20.0.

See

Was this documentation helpful?