Recall & Review
beginner
What is the purpose of handling specific exceptions in Python?
Handling specific exceptions allows your program to respond differently to different error types, making your code more robust and easier to debug.
Click to reveal answer
beginner
How do you catch a specific exception in Python?
Use a try-except block and specify the exception type after except, like: <br>
try:
# code
except ValueError:
# handle ValueErrorClick to reveal answer
intermediate
What happens if you catch a general exception instead of a specific one?
Catching a general exception (like Exception) can hide bugs and make it harder to find the real problem because it catches all errors, not just the ones you expect.
Click to reveal answer
intermediate
Can you handle multiple specific exceptions in one except block? How?
Yes, by using a tuple of exception types after except, like:<br>
except (TypeError, ValueError):
# handle both exceptionsClick to reveal answer
intermediate
What is the role of the else clause in try-except blocks?
The else clause runs code only if no exceptions were raised in the try block. It helps separate normal code from error handling.
Click to reveal answer
Which syntax correctly catches a ZeroDivisionError in Python?
✗ Incorrect
In Python, you catch specific exceptions using 'except ExceptionType:', so option C is correct.
What will happen if you write 'except:' without specifying an exception?
✗ Incorrect
An except block without an exception type catches all exceptions, which can hide bugs.
How do you handle both ValueError and TypeError in one except block?
✗ Incorrect
Use a tuple of exceptions in parentheses after except to catch multiple types.
What is the purpose of the else clause in a try-except block?
✗ Incorrect
The else clause runs only if the try block did not raise any exceptions.
Why is it better to catch specific exceptions rather than all exceptions?
✗ Incorrect
Catching specific exceptions helps handle errors properly and keeps bugs visible.
Explain how to handle multiple specific exceptions in Python and why it is useful.
Think about grouping exceptions in one except block.
You got /3 concepts.
Describe the role of the else clause in a try-except block and when you would use it.
It runs only when the try block succeeds.
You got /3 concepts.