Bird
Raised Fist0

Consider this code:

hard🚀 Application Q9 of Q15
Python - Exception Handling Fundamentals
Consider this code:
try:
    data = {'a': 1}
    print(data['b'])
except Exception as e:
    print('Error:', e)
finally:
    print('Done')

What will be the output?
AError: 'b' Done
B'b' Done
CDone
DError: KeyError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try block

    Accessing data['b'] raises KeyError because 'b' key does not exist.
  2. Step 2: Check exception handling and finally

    The generic except catches the error and prints 'Error:' plus the error message. Finally block always runs and prints 'Done'.
  3. Final Answer:

    Error: 'b' Done -> Option A
  4. Quick Check:

    Except prints error message; finally always runs [OK]
Quick Trick: Finally block runs regardless of exceptions [OK]
Common Mistakes:
MISTAKES
  • Expecting KeyError to print directly
  • Ignoring finally block output
  • Confusing error message format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes