Bird
Raised Fist0
Pythonprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the else block do in a try-except-else structure?
easy
A. Runs only if no error occurs in the try block
B. Runs only if an error occurs in the try block
C. Always runs regardless of errors
D. Runs before the try block

Solution

  1. Step 1: Understand try-except-else flow

    The try block runs code that might cause an error. If an error happens, the except block runs.
  2. Step 2: Role of else block

    The else block runs only if no error occurs in the try block, meaning the code succeeded without exceptions.
  3. Final Answer:

    Runs only if no error occurs in the try block -> Option A
  4. Quick Check:

    else runs if no error = A [OK]
Hint: Else runs only when try succeeds without errors [OK]
Common Mistakes:
  • Thinking else runs after except
  • Assuming else runs always
  • Confusing else with finally
2. Which of the following is the correct syntax for a try-except-else block in Python?
easy
A. try: pass finally: pass else: pass
B. try: pass else: pass except: pass
C. try: pass except: pass else: pass
D. except: pass try: pass else: pass

Solution

  1. Step 1: Recall correct order of blocks

    The correct order is try, then except, then else. The else block must come after except.
  2. Step 2: Check each option

    try: pass except: pass else: pass follows the correct order and syntax. Options A, B, and D have wrong order or misplaced blocks.
  3. Final Answer:

    try: pass except: pass else: pass -> Option C
  4. Quick Check:

    try-except-else order = C [OK]
Hint: Remember order: try, except, else [OK]
Common Mistakes:
  • Placing else before except
  • Using else after finally
  • Starting with except block
3. What will be the output of the following code?
try:
    print("Start")
    x = 1 / 1
except ZeroDivisionError:
    print("Error")
else:
    print("No Error")
print("End")
medium
A. Start\nNo Error\nEnd
B. Start\nEnd
C. Error\nNo Error\nEnd
D. Start\nError\nEnd

Solution

  1. Step 1: Analyze try block execution

    The code prints "Start" and calculates 1/1 which is 1, no error occurs.
  2. Step 2: Determine which blocks run

    Since no error, except block is skipped, else block runs printing "No Error", then "End" prints after.
  3. Final Answer:

    Start No Error End -> Option A
  4. Quick Check:

    No error means else runs = D [OK]
Hint: If no error, else runs after try [OK]
Common Mistakes:
  • Thinking except runs without error
  • Ignoring else block output
  • Missing that print("End") always runs
4. Identify the error in this code snippet:
try:
    print(10 / 0)
else:
    print("No error")
except ZeroDivisionError:
    print("Error occurred")
medium
A. Syntax is correct
B. except block is missing
C. try block is empty
D. else block is before except block

Solution

  1. Step 1: Check block order in try-except-else

    The correct order is try, except, then else. Here, else comes before except which is invalid syntax.
  2. Step 2: Confirm syntax error

    Python raises a syntax error because else must follow except, not precede it.
  3. Final Answer:

    else block is before except block -> Option D
  4. Quick Check:

    else must come after except = A [OK]
Hint: Else must follow except, not before [OK]
Common Mistakes:
  • Placing else before except
  • Forgetting except block
  • Misordering try-except-else blocks
5. Consider this code:
def check_value(val):
    try:
        result = 10 / val
    except ZeroDivisionError:
        return "Cannot divide by zero"
    else:
        return f"Result is {result}"

print(check_value(0))
print(check_value(5))
What is the output?
hard
A. Result is 0.0\nCannot divide by zero
B. Cannot divide by zero\nResult is 2.0
C. Cannot divide by zero\nResult is 0
D. Error at runtime

Solution

  1. Step 1: Analyze call with 0

    When val=0, division causes ZeroDivisionError, so except block returns "Cannot divide by zero".
  2. Step 2: Analyze call with 5

    When val=5, division succeeds (10/5=2.0), so else block returns "Result is 2.0".
  3. Final Answer:

    Cannot divide by zero Result is 2.0 -> Option B
  4. Quick Check:

    ZeroDivision triggers except, else runs if no error = B [OK]
Hint: Except returns on error; else returns on success [OK]
Common Mistakes:
  • Assuming else runs even if error occurs
  • Confusing output order
  • Expecting runtime error instead of handled exception