Bird
0
0

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

easy📝 Syntax Q12 of 15
Python - Exception Handling Fundamentals
Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
Atry: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero')
Btry: x = 1/0 catch ZeroDivisionError: print('Cannot divide by zero')
Ctry: x = 1/0 except: print('Error') finally ZeroDivisionError:
Dtry: x = 1/0 except ZeroDivisionError then: print('Cannot divide by zero')
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct try-except syntax

    Python uses try: followed by except ExceptionType: to catch errors.
  2. Step 2: Check each option for syntax errors

    try: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero') uses correct except ZeroDivisionError: syntax; others use invalid keywords like catch or incorrect formatting.
  3. Final Answer:

    try: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero') -> Option A
  4. Quick Check:

    Correct except syntax = A [OK]
Quick Trick: Use 'except ExceptionType:' to catch specific errors [OK]
Common Mistakes:
  • Using 'catch' instead of 'except'
  • Adding 'then' after except
  • Misplacing 'finally' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes