Challenge - 5 Problems
Try-Except-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of try-except-else with no exception
What is the output of this Python code?
Python
try: print("Start") except ValueError: print("Error") else: print("No Error") print("End")
Attempts:
2 left
💡 Hint
The else block runs only if no exception occurs in try.
✗ Incorrect
Since no exception is raised, the try block prints 'Start', then else block prints 'No Error', and finally 'End' is printed.
❓ Predict Output
intermediate2:00remaining
Output when exception is caught in try-except-else
What is the output of this code?
Python
try: x = int('abc') except ValueError: print('Caught ValueError') else: print('No Error') print('Done')
Attempts:
2 left
💡 Hint
The else block does not run if an exception is caught.
✗ Incorrect
The int conversion raises ValueError, so except block runs printing 'Caught ValueError'. The else block is skipped. Then 'Done' is printed.
❓ Predict Output
advanced2:00remaining
Behavior of try-except-else with finally
What is the output of this code snippet?
Python
try: print('Try block') except Exception: print('Except block') else: print('Else block') finally: print('Finally block')
Attempts:
2 left
💡 Hint
Finally block always runs after try-except-else.
✗ Incorrect
No exception occurs, so try prints 'Try block', else prints 'Else block', and finally prints 'Finally block'.
❓ Predict Output
advanced2:00remaining
Output when exception not caught by except
What happens when this code runs?
Python
try: print(10 / 0) except ValueError: print('ValueError caught') else: print('No Error') print('After try-except')
Attempts:
2 left
💡 Hint
Only ValueError is caught, ZeroDivisionError is not handled here.
✗ Incorrect
ZeroDivisionError occurs but except only catches ValueError, so error propagates and program stops before last print.
🧠 Conceptual
expert3:00remaining
Understanding try-except-else-finally execution order
Consider this code:
try:
print('A')
except Exception:
print('B')
else:
print('C')
finally:
print('D')
What is the order of printed lines when no exception occurs?Attempts:
2 left
💡 Hint
Else runs only if no exception, finally always runs last.
✗ Incorrect
When no exception occurs, try prints 'A', else prints 'C', finally prints 'D'. So order is A, C, D.