Repaso
EspaƱol
PythonCorePY-39

Why is a bare except: considered harmful?

Options

  1. It is a syntax error in Python 3, so the program will not run at all
  2. It also catches KeyboardInterrupt and SystemExit, so Ctrl+C fails (Correct option)
  3. It is much slower, because Python checks every type of exception
  4. It only catches ValueError, so every other error passes through

Answer

It also catches KeyboardInterrupt and SystemExit, making programs hard to stop

Why

A bare except: catches EVERYTHING, including Ctrl+C (KeyboardInterrupt) and sys.exit() (SystemExit). Your program may become impossible to stop, and real bugs are hidden.

Catch only the errors you expect (for example except ValueError:), or at most except Exception: and log the error.

Remember

Catch specific exceptions, never everything.

Key terms

Practice this question in Repaso