Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Exception Handling Fundamentals
What will be the output of this code?
try:
    x = int('abc')
except ValueError:
    print('ValueError caught')
except Exception:
    print('General exception caught')
ANo output, program crashes
BGeneral exception caught
CValueError caught
DBoth messages printed
Step-by-Step Solution
Solution:
  1. Step 1: Identify the exception raised

    int('abc') raises a ValueError because 'abc' cannot be converted to int.
  2. Step 2: Determine which except block runs

    The first except block matches ValueError, so it runs and prints 'ValueError caught'.
  3. Final Answer:

    ValueError caught -> Option C
  4. Quick Check:

    Specific except block runs first [OK]
Quick Trick: Specific exceptions catch errors before general ones [OK]
Common Mistakes:
  • Thinking general Exception block runs first
  • Expecting both except blocks to run
  • Assuming program crashes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes