Bird
Raised Fist0
Pythonprogramming~20 mins

Try–except execution flow 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 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.

Practice

(1/5)
1. What is the main purpose of using a try-except block in Python?
easy
A. To speed up the program execution
B. To handle errors and prevent the program from crashing
C. To repeat a block of code multiple times
D. To define a new function

Solution

  1. Step 1: Understand the role of try block

    The try block contains code that might cause an error during execution.
  2. Step 2: Understand the role of except block

    The except block catches and handles the error so the program does not stop abruptly.
  3. Final Answer:

    To handle errors and prevent the program from crashing -> Option B
  4. Quick Check:

    Try-except handles errors = B [OK]
Hint: Try-except blocks catch errors to keep programs running [OK]
Common Mistakes:
  • Thinking try-except speeds up code
  • Confusing try-except with loops
  • Using try-except to define functions
2. Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
easy
A. try: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero')
B. try: x = 1/0 catch ZeroDivisionError: print('Cannot divide by zero')
C. try: x = 1/0 except: print('Error') finally ZeroDivisionError:
D. try: x = 1/0 except ZeroDivisionError then: print('Cannot divide by zero')

Solution

  1. Step 1: Identify correct try-except syntax

    Python uses try: followed by except ExceptionType: to catch errors.
  2. Step 2: Check each option for syntax errors

    try: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero') uses correct except ZeroDivisionError: syntax; others use invalid keywords like catch or incorrect formatting.
  3. Final Answer:

    try: x = 1/0 except ZeroDivisionError: print('Cannot divide by zero') -> Option A
  4. Quick Check:

    Correct except syntax = A [OK]
Hint: Use 'except ExceptionType:' to catch specific errors [OK]
Common Mistakes:
  • Using 'catch' instead of 'except'
  • Adding 'then' after except
  • Misplacing 'finally' keyword
3. What will be the output of the following code?
try:
    print('Start')
    x = 5 / 0
    print('End')
except ZeroDivisionError:
    print('Error caught')
print('Done')
medium
A. Start End Error caught Done
B. Error caught Done
C. Start Done
D. Start Error caught Done

Solution

  1. Step 1: Trace code inside try block

    It prints 'Start', then tries to divide 5 by 0, which raises ZeroDivisionError before printing 'End'.
  2. Step 2: Handle exception and continue

    The except block catches the error and prints 'Error caught'. After that, the program continues and prints 'Done'.
  3. Final Answer:

    Start Error caught Done -> Option D
  4. Quick Check:

    Exception stops try block, except runs = C [OK]
Hint: Error stops try; except runs; code after try-except runs [OK]
Common Mistakes:
  • Assuming 'End' prints after error
  • Missing that except block runs
  • Thinking program stops after error
4. Identify the error in this code snippet:
try:
    print('Hello')
except ValueError
    print('Value error occurred')
medium
A. try block cannot have print statements
B. Missing parentheses after except
C. Missing colon ':' after except ValueError
D. except block must come before try block

Solution

  1. Step 1: Check syntax of except statement

    The except line must end with a colon ':' to define the block.
  2. Step 2: Identify missing colon

    In the code, except ValueError is missing the colon, causing a syntax error.
  3. Final Answer:

    Missing colon ':' after except ValueError -> Option C
  4. Quick Check:

    except line needs ':' = A [OK]
Hint: Always put ':' after except ExceptionType [OK]
Common Mistakes:
  • Forgetting colon after except
  • Adding parentheses after except
  • Misordering try and except blocks
5. You want to safely convert user input to an integer, using try-except-else and catching ValueError specifically, printing 'Invalid input' for invalid input and 'Input is', num for valid input, without stopping the program. Which code does this?
hard
A. try: num = int(input('Enter number: ')) except ValueError: print('Invalid input') else: print('Input is', num)
B. try: num = int(input('Enter number: ')) except: print('Invalid input') else: print('Input is', num)
C. try: num = int(input('Enter number: ')) except ValueError: print('Invalid input') finally: print('Done')
D. try: num = int(input('Enter number: ')) except ValueError: print('Invalid input')

Solution

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

    The try block attempts conversion; except handles errors; else runs if no error occurs.
  2. Step 2: Check which option prints 'Invalid input' on error and shows input if valid

    try: num = int(input('Enter number: ')) except ValueError: print('Invalid input') else: print('Input is', num) correctly prints 'Invalid input' on ValueError and prints the number if conversion succeeds.
  3. Final Answer:

    try: num = int(input('Enter number: ')) except ValueError: print('Invalid input') else: print('Input is', num) -> Option A
  4. Quick Check:

    Use except for errors, else for success = D [OK]
Hint: Use except for errors and else for success actions [OK]
Common Mistakes:
  • Not using else to handle successful input
  • Catching all exceptions without specifying
  • Missing error handling causing crash