no-silent-exception

Try in Playground
python-best-practicesBest PracticeWarning

0

No tags

No CWE or CVE

Never ignore an exception.

Ast Rule: try block


no-silent-exception

How to write a rule
function visit(node, filename, code) {
  const allClausesWithPass = node.exceptClauses.filter(e => e.getCodeBlock() && e.getCodeBlock() === "pass");
  allClausesWithPass.forEach(c => {
    const error = buildError(c.start.line, c.start.col, c.end.line, c.end.col, "silent exception", "WARNING", "BEST_PRACTICES");
    addError(error);
  });
}

pass-only-in-exception.py

Expected test result: has error

Make the exception silent and do not log anything, missing potential information.

a = 2
b = 0
try:
  c = a /b
except Exception as e:
  pass

do-not-ignore-exception.py

Expected test result: no error

a = 2
b = 0
try:
  c = a /b
except ValueError as e:
  print(e)
  pass
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Explore
  • Cookbooks
  • Playground
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.