0
0
Pythonprogramming~20 mins

Try–except execution flow in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Try-Except Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of try-except with no error
What is the output of this code when no error occurs?
Python
try:
    print("Start")
    result = 10 / 2
    print("Result is", result)
except ZeroDivisionError:
    print("Division by zero!")
finally:
    print("End")
ADivision by zero!\nEnd
BStart\nDivision by zero!\nEnd
CStart\nEnd
DStart\nResult is 5.0\nEnd
Attempts:
2 left
💡 Hint
Remember that except block runs only if an error occurs.
Predict Output
intermediate
2:00remaining
Output when exception occurs and is caught
What is the output of this code when a ZeroDivisionError occurs?
Python
try:
    print("Start")
    result = 10 / 0
    print("Result is", result)
except ZeroDivisionError:
    print("Division by zero!")
finally:
    print("End")
AStart\nDivision by zero!\nEnd
BStart\nResult is 0\nEnd
CDivision by zero!\nEnd
DStart\nEnd
Attempts:
2 left
💡 Hint
Division by zero causes an exception, so except block runs.
Predict Output
advanced
2:00remaining
Output with multiple except blocks
What is the output of this code?
Python
try:
    print("Start")
    x = int('abc')
    print("Parsed", x)
except ValueError:
    print("Value error caught")
except ZeroDivisionError:
    print("Zero division error caught")
finally:
    print("End")
AStart\nEnd
BStart\nValue error caught\nEnd
CStart\nParsed abc\nEnd
DStart\nZero division error caught\nEnd
Attempts:
2 left
💡 Hint
int('abc') raises ValueError.
Predict Output
advanced
2:00remaining
Output when exception not caught
What happens when an exception is raised but not caught by except blocks?
Python
try:
    print("Start")
    x = [1, 2, 3]
    print(x[5])
except ZeroDivisionError:
    print("Zero division error caught")
finally:
    print("End")
AStart\nEnd\nIndexError exception raised
BStart\nZero division error caught\nEnd
CStart\nEnd
DIndexError exception raised
Attempts:
2 left
💡 Hint
IndexError is not caught by ZeroDivisionError except block.
🧠 Conceptual
expert
2:00remaining
Order of execution in try-except-finally
Which option correctly describes the order of execution when an exception occurs inside the try block and is caught?
Afinally block runs first, then try block, then except block
Bexcept block runs first, then try block, then finally block
Ctry block runs until exception, except block runs, finally block runs
Dtry block runs fully, finally block runs, except block runs
Attempts:
2 left
💡 Hint
Think about what happens when an error occurs inside try.