Bird
0
0

You want to catch all exceptions except KeyboardInterrupt and SystemExit. Which is the best way to write the except block?

hard📝 Application Q15 of 15
Python - Custom Exceptions
You want to catch all exceptions except KeyboardInterrupt and SystemExit. Which is the best way to write the except block?
Aexcept Exception:
Bexcept BaseException:
Cexcept (KeyboardInterrupt, SystemExit):
Dexcept (Exception, KeyboardInterrupt, SystemExit):
Step-by-Step Solution
Solution:
  1. Step 1: Recall exception hierarchy for KeyboardInterrupt and SystemExit

    Both KeyboardInterrupt and SystemExit inherit directly from BaseException, not Exception.
  2. Step 2: Choose except block that excludes these exceptions

    except Exception: catches all exceptions except KeyboardInterrupt and SystemExit, which is the desired behavior.
  3. Final Answer:

    except Exception: -> Option A
  4. Quick Check:

    Exception excludes system-exiting exceptions [OK]
Quick Trick: Use except Exception to exclude system-exiting exceptions [OK]
Common Mistakes:
  • Using except BaseException catches everything including system exit
  • Catching KeyboardInterrupt and SystemExit explicitly when not needed
  • Combining Exception with KeyboardInterrupt in except tuple

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes