Bird
0
0

Consider this code:

hard📝 Application Q15 of 15
Python - Advanced Exception Handling
Consider this code:
def test():
    try:
        return 'try'
    except:
        return 'except'
    finally:
        return 'finally'

result = test()
print(result)
What will be printed?
Afinally
Bexcept
CNone
Dtry
Step-by-Step Solution
Solution:
  1. Step 1: Understand return in try and finally

    The try block returns 'try', but the finally block also has a return statement.
  2. Step 2: Know that finally return overrides others

    In Python, if finally has a return, it overrides any previous return from try or except.
  3. Step 3: Determine final output

    The function returns 'finally', so print(result) outputs 'finally'.
  4. Final Answer:

    finally -> Option A
  5. Quick Check:

    finally return overrides try/except returns = D [OK]
Quick Trick: Return in finally overrides try/except returns. [OK]
Common Mistakes:
  • Thinking try return is final
  • Ignoring finally's return effect
  • Assuming except runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes