0
0
Pythonprogramming~20 mins

Handling specific exceptions in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of specific exception handling
What is the output of this code when the input is 0?
Python
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
except Exception:
    print("Some other error")
ANo output
BSome other error
C0
DCannot divide by zero
Attempts:
2 left
💡 Hint
Think about which exception is raised by dividing by zero.
Predict Output
intermediate
2:00remaining
Output when catching multiple exceptions
What will this code print?
Python
try:
    x = int('abc')
except (ValueError, TypeError):
    print("Caught ValueError or TypeError")
except Exception:
    print("Caught some other exception")
ACaught some other exception
BCaught ValueError or TypeError
Cabc
DNo output
Attempts:
2 left
💡 Hint
What exception does int('abc') raise?
Predict Output
advanced
2:00remaining
Exception handling with else and finally
What is the output of this code?
Python
try:
    print("Start")
    x = 5 / 1
except ZeroDivisionError:
    print("Divide by zero error")
else:
    print("No error occurred")
finally:
    print("Always runs")
A
Start
No error occurred
Always runs
B
Start
Divide by zero error
Always runs
C
No error occurred
Always runs
D
Start
Always runs
Attempts:
2 left
💡 Hint
Consider what happens when no exception is raised.
Predict Output
advanced
2:00remaining
What error is raised?
What error does this code raise?
Python
try:
    d = {'a': 1}
    print(d['b'])
except KeyError:
    print("Key not found")
except Exception:
    print("Other error")
ANone
BOther error
CKey not found
DKeyError exception not caught
Attempts:
2 left
💡 Hint
What happens when you access a missing key in a dictionary?
Predict Output
expert
2:00remaining
Output with nested try-except and re-raising
What is the output of this code?
Python
try:
    try:
        x = 1 / 0
    except ZeroDivisionError:
        print("Inner catch")
        raise
except ZeroDivisionError:
    print("Outer catch")
A
Inner catch
Outer catch
BInner catch
COuter catch
DNo output
Attempts:
2 left
💡 Hint
What happens when an exception is re-raised inside an except block?