Bird
0
0

You want to catch all exceptions except KeyboardInterrupt and SystemExit. Which of the following except clauses is the best practice?

hard📝 Application Q8 of 15
Python - Custom Exceptions
You want to catch all exceptions except KeyboardInterrupt and SystemExit. Which of the following except clauses is the best practice?
Aexcept Exception:
Bexcept BaseException:
Cexcept (KeyboardInterrupt, SystemExit):
Dexcept:
Step-by-Step Solution
Solution:
  1. Step 1: Understand Exception Classes

    BaseException is the root of all exceptions including system-exiting ones like KeyboardInterrupt and SystemExit. Exception excludes these system-exiting exceptions.
  2. Step 2: Analyze Options

    except Exception: catches all exceptions derived from Exception, excluding system-exiting exceptions. except BaseException: catches everything including system-exiting exceptions, which is not recommended. except (KeyboardInterrupt, SystemExit): catches only the system-exiting exceptions, not others. except: is equivalent to catching BaseException.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use except Exception to exclude system-exiting exceptions [OK]
Quick Trick: Use except Exception to exclude system-exiting exceptions [OK]
Common Mistakes:
  • Using except BaseException which catches system-exiting exceptions
  • Using bare except which is discouraged
  • Catching only KeyboardInterrupt and SystemExit without others

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes