Why is a bare except: considered harmful?
Options
- It is a syntax error in Python 3, so the program will not run at all
- It also catches
KeyboardInterruptandSystemExit, so Ctrl+C fails (Correct option) - It is much slower, because Python checks every type of exception
- 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.