Skip to main content

SKY-L002 · Logic and resilience

Bare except block

Medium

The catalog gives no severity for this rule; the detector always reports MEDIUM.

An except: with no exception type catches everything, including KeyboardInterrupt and SystemExit. Those are not errors: they are how Ctrl+C and sys.exit() stop a program.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A bare except: around a slow call swallows the stop request, so Ctrl+C or a worker shutdown is logged as a cache error and the program keeps running. It also hides real bugs, such as a NameError, behind the same fallback.

What Skylos flags

rates/cache.py · PythonReported on line 11
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__)  def cached_rate(cache, currency):    try:        return cache.get(f"rate:{currency}")    except:        logger.warning("rate cache unavailable, falling back to the API")        return None

What Skylos leaves alone

rates/cache.py · Python
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__)  def cached_rate(cache, currency):    try:        return cache.get(f"rate:{currency}")    except Exception:        logger.warning("rate cache unavailable, falling back to the API")        return None

Why Skylos leaves it alone

except Exception: is still broad, but it does not catch KeyboardInterrupt, SystemExit or GeneratorExit, so the program can still be stopped. SKY-L002 only looks for a handler with no type at all. A narrower type, as in the fix, is better still.

How Skylos decides

  1. Runs with --quality on Python files. Visits every except clause, including except* groups.
  2. Reports MEDIUM when the clause names no exception type (except:). Any named type, even except BaseException:, is not reported by this rule.
  3. Does not look at the handler body: a bare except: that logs, cleans up or re-raises is reported the same way. The finding is on the except line.

How to fix it

Catch the errors the call can actually raise. Here the Redis client raises connection and timeout errors when the cache is down.

rates/cache.py · Python · the fix, against the flagged file
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__)  def cached_rate(cache, currency):    try:        return cache.get(f"rate:{currency}")Removed:     except:Added:     except (ConnectionError, TimeoutError):        logger.warning("rate cache unavailable, falling back to the API")        return None

When it can be wrong

  • A bare except: that ends with raise passes every exception on, so it swallows nothing; it is still reported. except BaseException: with raise says the same thing more clearly and is not reported.
  • A bare except: pass or except: return None is also reported by SKY-L007 (empty error handler). SKY-L030 does not report bare handlers; it covers except Exception: with a placeholder return.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the except: line.

    Python
    except:  # skylos: ignore[SKY-L002] re-raised below after cleanup
  • # noqa: E722, the flake8 code for the same problem, is not read: Skylos ignores noqa comments that name codes. A bare # noqa with no code does hide the finding.

  • [tool.skylos] ignore = ["SKY-L002"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.