Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Python - Exception Handling Fundamentals
What is wrong with this code?
try:
    x = int('5')
except ValueError:
    print('Value error')
else:
    print('No error')
ANo error, code runs and prints 'No error'
BValueError will be raised and caught
CSyntax error due to else block
Delse block should be except block
Step-by-Step Solution
Solution:
  1. Step 1: Analyze int('5')

    int('5') converts string '5' to integer 5 without error.
  2. Step 2: Understand try-except-else flow

    Since no exception occurs, else block runs printing 'No error'.
  3. Final Answer:

    No error, code runs and prints 'No error' -> Option A
  4. Quick Check:

    Successful try runs else block [OK]
Quick Trick: else runs only if no exception occurs [OK]
Common Mistakes:
  • Thinking else is syntax error
  • Expecting ValueError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes