Bird
0
0

Which of the following try-except blocks correctly catches all exceptions derived from Exception but excludes SystemExit and KeyboardInterrupt?

easy📝 Syntax Q3 of 15
Python - Custom Exceptions
Which of the following try-except blocks correctly catches all exceptions derived from Exception but excludes SystemExit and KeyboardInterrupt?
Atry: pass except Exception: pass
Btry: pass except BaseException: pass
Ctry: pass except SystemExit: pass
Dtry: pass except KeyboardInterrupt: pass
Step-by-Step Solution
Solution:
  1. Step 1: Understand Exception Hierarchy

    Exception is a subclass of BaseException that excludes system-exiting exceptions like SystemExit and KeyboardInterrupt.
  2. Step 2: Analyze Options

    try: pass except Exception: pass catches all exceptions derived from Exception, excluding system-exiting ones. try: pass except BaseException: pass catches all exceptions including system-exiting. Options C and D catch only specific exceptions.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Catch Exception to exclude system-exiting exceptions [OK]
Quick Trick: Catch Exception to exclude system-exiting exceptions [OK]
Common Mistakes:
  • Using BaseException to catch all exceptions including system-exiting
  • Catching only SystemExit or KeyboardInterrupt separately
  • Assuming Exception includes system-exiting exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes