Bird
Raised Fist0

Which of the following code snippets correctly catches all exceptions using a generic handler in Python?

easy📝 Syntax Q3 of Q15
Python - Exception Handling Fundamentals
Which of the following code snippets correctly catches all exceptions using a generic handler in Python?
Atry: pass except: print('Error')
Btry: pass except Exception: print('Error')
Ctry: pass except Error: print('Error')
Dtry: pass catch Exception: print('Error')
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for Generic Exception

    The correct syntax to catch all exceptions derived from Exception is except Exception:.
  2. Step 2: Analyze Options

    try: pass except Exception: print('Error') uses except Exception: correctly. try: pass except: print('Error') uses bare except: which catches all exceptions including system-exit, but is discouraged. try: pass except Error: print('Error') uses an undefined Error class. try: pass catch Exception: print('Error') uses invalid keyword catch.
  3. Final Answer:

    try: pass except Exception: print('Error') is the correct syntax.
  4. Quick Check:

    Use except Exception: to catch most errors [OK]
Quick Trick: Use 'except Exception:' for generic catching [OK]
Common Mistakes:
MISTAKES
  • Using bare except without Exception
  • Using invalid keywords like catch
  • Catching undefined exception classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes