Challenge - 5 Problems
Try-Except Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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")
Attempts:
2 left
💡 Hint
Remember that except block runs only if an error occurs.
✗ Incorrect
Since 10 divided by 2 is valid, no exception occurs. So the try block runs fully, printing 'Start' and 'Result is 5.0'. The finally block always runs, printing 'End'.
❓ Predict Output
intermediate2: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")
Attempts:
2 left
💡 Hint
Division by zero causes an exception, so except block runs.
✗ Incorrect
The division by zero raises ZeroDivisionError, so the except block prints 'Division by zero!'. The finally block always runs, printing 'End'.
❓ Predict Output
advanced2: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")
Attempts:
2 left
💡 Hint
int('abc') raises ValueError.
✗ Incorrect
Trying to convert 'abc' to int raises ValueError, so the first except block runs. The finally block always runs.
❓ Predict Output
advanced2: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")
Attempts:
2 left
💡 Hint
IndexError is not caught by ZeroDivisionError except block.
✗ Incorrect
Accessing x[5] raises IndexError, which is not caught. The finally block runs printing 'End'. Then the program raises IndexError.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about what happens when an error occurs inside try.
✗ Incorrect
When an exception occurs, the try block stops at the error, the except block runs to handle it, then the finally block runs always.