Python - Exception Handling FundamentalsWhich 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')Check Answer
Step-by-Step SolutionSolution:Step 1: Recall Python exception syntaxPython uses try-except blocks with 'except ExceptionType:' syntax.Step 2: Identify correct exception name and syntaxZeroDivisionError is the correct exception name; 'except ZeroDivisionError:' is valid.Final Answer:try-except with except ZeroDivisionError: -> Option CQuick 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 nameWrong module prefix
Master "Exception Handling Fundamentals" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Encapsulation and Data Protection - Getter and setter methods - Quiz 2easy Exception Handling Fundamentals - Try–except execution flow - Quiz 12easy Exception Handling Fundamentals - Generic exception handling - Quiz 3easy File Handling Fundamentals - File path handling - Quiz 13medium File Handling Fundamentals - Opening and closing files - Quiz 6medium File Handling Fundamentals - Reading file data - Quiz 5medium File Handling Fundamentals - Why file handling is required - Quiz 12easy File Handling Fundamentals - Reading file data - Quiz 13medium Modules and Code Organization - Why modules are needed - Quiz 15hard Object-Oriented Programming Foundations - Real-world modeling using objects - Quiz 6medium