0
0
Pythonprogramming~20 mins

Try–except–else behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Try-Except-Else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
AStart\nNo Error\nEnd
BStart\nError\nNo Error\nEnd
CStart\nError\nEnd
DNo Error\nEnd
Attempts:
2 left
💡 Hint
The else block runs only if no exception occurs in try.
Predict Output
intermediate
2: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')
ANo Error\nDone
BCaught ValueError\nDone
CDone
DCaught ValueError\nNo Error\nDone
Attempts:
2 left
💡 Hint
The else block does not run if an exception is caught.
Predict Output
advanced
2: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')
AFinally block
BTry block\nExcept block\nFinally block
CTry block\nFinally block
DTry block\nElse block\nFinally block
Attempts:
2 left
💡 Hint
Finally block always runs after try-except-else.
Predict Output
advanced
2: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')
AZeroDivisionError is raised, program stops before printing 'After try-except'
BValueError caught\nAfter try-except
CNo Error\nAfter try-except
DAfter try-except
Attempts:
2 left
💡 Hint
Only ValueError is caught, ZeroDivisionError is not handled here.
🧠 Conceptual
expert
3: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?
AA, D, C
BA, B, C, D
CA, C, D
DD, A, C
Attempts:
2 left
💡 Hint
Else runs only if no exception, finally always runs last.