Python Exception Handling Quiz
A 35-question quiz covering try-except blocks, handling multiple errors, using else and finally, and creating custom exceptions.
Question 1
What is the primary purpose of a `try-except` block?
Question 2
Analyze the code below. What is the output?
try:
print(1 / 0)
except ZeroDivisionError:
print("Caught")Question 3
Which block is executed ONLY if NO exception is raised in the `try` block?
Question 4
Which block is executed ALWAYS, regardless of whether an exception occurred or not?
Question 5
Analyze the code below. What happens?
try:
x = int("hello")
except ValueError:
print("Value Error")
except TypeError:
print("Type Error")Question 6
How do you catch multiple exceptions in a single `except` line?
Question 7
What keyword is used to manually trigger an exception?
Question 8
Analyze the code below. What is the output?
try:
print("A")
raise Exception("Error")
print("B")
except:
print("C")Question 9
Why is `except:` (bare except) generally discouraged?
Question 10
Which class should user-defined exceptions inherit from?
Question 11
Analyze the code below. What is `e`?
try:
1/0
except ZeroDivisionError as e:
print(type(e))Question 12
What happens if an exception is raised inside a `finally` block?
Question 13
Analyze the code below. What is the output?
def func():
try:
return 1
finally:
return 2
print(func())Question 14
Which exception is raised when accessing a dictionary key that doesn't exist?
Question 15
Can you have a `try` block without an `except` block?
Question 16
Analyze the code below. What is the output?
try:
print("Start")
except:
print("Error")
else:
print("Success")Question 17
What is the parent class of `ValueError` and `TypeError`?
Question 18
How do you re-raise the active exception inside an `except` block?
Question 19
Analyze the code below. What is the output?
try:
raise ValueError("Wrong")
except ValueError as e:
print(e)Question 20
Which exception is raised when an import fails?
Question 21
What is the purpose of the `assert` statement?
Question 22
Analyze the code below. What is the output?
try:
print("Try")
exit()
finally:
print("Finally")Question 23
Can you define your own exception hierarchy?
Question 24
Analyze the code below. Which except block catches the error?
try:
raise IndexError()
except LookupError:
print("Lookup")
except IndexError:
print("Index")Question 25
What is `sys.exc_info()` used for?
Question 26
Analyze the code below. What happens?
try:
pass
except:
print("Error")
else:
print("Else")
finally:
print("Finally")Question 27
Which exception is raised when a function receives an argument of the correct type but inappropriate value?
Question 28
What is exception chaining (Python 3)?
Question 29
Analyze the code below. What is the output?
try:
[][0]
except (ValueError, IndexError):
print("Caught")Question 30
Which exception is raised when you try to access an attribute that doesn't exist?
Question 31
Can you raise an instance of a class that does not inherit from BaseException?
Question 32
Analyze the code below. What is the output?
try:
print("1")
except:
print("2")
finally:
print("3")Question 33
What is the `args` attribute of an exception?
Question 34
Analyze the code below. What happens?
try:
x = 1/0
except ZeroDivisionError:
pass
print("Done")Question 35
Why is it important to clean up resources in a `finally` block?
