Practice mode Python

Why does except Exception: not catch a KeyboardInterrupt raised by pressing Ctrl+C?

Correct answer C. KeyboardInterrupt inherits from BaseException, not from Exception

Explanation

BaseException is the root of the exception hierarchy. KeyboardInterrupt, SystemExit and GeneratorExit derive directly from it so that generic except Exception handlers do not accidentally swallow requests to stop the program. A bare except: or except BaseException: would catch it.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.