Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
Python - Advanced Exception Handling

What will be the output of this code snippet?

try:
    try:
        float('xyz')
    except ValueError as ve:
        raise RuntimeError('Conversion error') from ve
except Exception as exc:
    print(type(exc).__name__)
    print(exc.__cause__)
ARuntimeError ValueError("could not convert string to float: 'xyz'")
BValueError RuntimeError('Conversion error')
CRuntimeError None
DValueError None
Step-by-Step Solution
Solution:
  1. Step 1: Inner try-except raises RuntimeError from ValueError

    The float('xyz') raises ValueError, caught and chained to RuntimeError.
  2. Step 2: Outer except prints exception type and cause

    Prints RuntimeError and the original ValueError as cause.
  3. Final Answer:

    RuntimeError ValueError("could not convert string to float: 'xyz'") -> Option A
  4. Quick Check:

    Check exception type and __cause__ attribute [OK]
Quick Trick: Exception __cause__ holds original exception instance [OK]
Common Mistakes:
  • Printing exception type instead of message
  • Assuming __cause__ is None without 'from'
  • Mixing up exception types and messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes