Bird
0
0

Which of the following is the correct syntax to catch a ZeroDivisionError in Python?

easy📝 Syntax Q3 of 15
Python - Exception Handling Fundamentals
Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
Atry:\n x = 1 / 0\ncatch ZeroDivisionError:\n print('Cannot divide by zero')
Btry:\n x = 1 / 0\nexcept error.ZeroDivisionError:\n print('Cannot divide by zero')
Ctry:\n x = 1 / 0\nexcept ZeroDivisionError:\n print('Cannot divide by zero')
Dtry:\n x = 1 / 0\nexcept ZeroDivision:\n print('Cannot divide by zero')
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python exception syntax

    Python uses try-except blocks with 'except ExceptionType:' syntax.
  2. Step 2: Identify correct exception name and syntax

    ZeroDivisionError is the correct exception name; 'except ZeroDivisionError:' is valid.
  3. Final Answer:

    try-except with except ZeroDivisionError: -> Option C
  4. Quick Check:

    Correct try-except syntax = try:\n x = 1 / 0\nexcept ZeroDivisionError:\n print('Cannot divide by zero') [OK]
Quick Trick: Use 'except ExceptionType:' to catch exceptions [OK]
Common Mistakes:
  • Using 'catch' instead of 'except'
  • Wrong exception name
  • Wrong module prefix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes