PCAP Exceptions and File I/O Practice Question
A developer needs to write a function that safely divides two numbers and returns None if division by zero occurs. Which implementation is most Pythonic?
⚠ Common exam trap
Python Institute often tests the distinction between EAFP and LBYL, and the trap here is that candidates may choose Option C (LBYL) because it appears safer, or Option D because they confuse ValueError with ZeroDivisionError, not realizing that Python raises a specific exception for division by zero.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
def safe_divide(a, b): try: return a/b except ZeroDivisionError: return None
It uses a try-except block to catch the specific ZeroDivisionError that Python raises when dividing by zero. This is the most Pythonic approach, following the EAFP (Easier to Ask for Forgiveness than Permission) principle, which is preferred over LBYL (Look Before You Leap) in Python. The function returns None only when the exception occurs, preserving the natural behavior of division for all other cases.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
def safe_divide(a, b): try: return a/b except ZeroDivisionError: return None
Why this is correct
This is the canonical EAFP implementation: it attempts the division inside a try block and catches only the exact exception that signals division by zero. ZeroDivisionError is a subclass of ArithmeticError and is raised whenever a numeric denominator evaluates to zero, so returning None is a clean and specific fallback. Any other exception (such as TypeError for incompatible operands) is deliberately allowed to propagate, preserving the caller's ability to debug the real error.
- ✗
def safe_divide(a, b): return a/b if b else None
Why it's wrong here
Using `b` as the condition relies on Python truthiness rather than catching the arithmetic exception. For built-in numeric types every zero value (0, 0.0, Decimal('0'), etc.) is falsy, so a zero denominator does produce the intended None; however, this approach silently turns non-numeric falsy denominators such as None, '' or [] into a None return instead of the TypeError that direct division would raise. It therefore hides type errors and bypasses the explicit ZeroDivisionError handling that the try/except version provides.
- ✗
def safe_divide(a, b): if b != 0: return a/b else: return None
Why it's wrong here
This LBYL-style guard works only for built-in numeric equality with zero and fails to provide the safety implied by the problem statement. It does not catch TypeError for unsupported operand types, and the explicit `b != 0` check can be incorrect or misleading for custom numeric objects whose `__eq__` or `__ne__` methods are non-standard. Additionally, separating the check from the operation is not exception-safe in concurrent situations, because b's value could change between the comparison and the division; the EAFP version keeps the operation atomic.
- ✗
def safe_divide(a, b): try: return a/b except ValueError: return None
Why it's wrong here
ZeroDivisionError is not caught because it is not a ValueError; division by zero raises ZeroDivisionError, a subclass of ArithmeticError. Since the except clause names ValueError, the ZeroDivisionError propagates unhandled and the function will crash when b is zero. The correct exception is ZeroDivisionError, and catching a broader ArithmeticError would also work, but ValueError is unrelated to any exception the division operator can raise for zero denominators.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.