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:
except (TypeError, ValueError):
# handle both exceptions
Click 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?
Atry: ... except error ZeroDivisionError: ...
Btry: ... catch ZeroDivisionError: ...
Ctry: ... except ZeroDivisionError: ...
Dtry: ... except: ZeroDivisionError ...
✗ 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?
AIt catches all exceptions.
BIt catches only syntax errors.
CIt causes a syntax error.
DIt catches no exceptions.
✗ 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?
Aexcept ValueError & TypeError:
Bexcept ValueError or TypeError:
Cexcept ValueError, TypeError:
Dexcept (ValueError, TypeError):
✗ 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?
ARuns code only if an exception occurs.
BRuns code only if no exception occurs.
CRuns code before the try block.
DRuns code after finally 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?
ATo handle errors more precisely and avoid hiding bugs.
BTo hide bugs.
CTo avoid writing except blocks.
DTo make the program slower.
✗ 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.
Practice
(1/5)
1. What is the main purpose of using try-except blocks in Python?
easy
A. To catch and handle specific errors so the program doesn't crash
B. To speed up the program execution
C. To write comments inside the code
D. To create new functions automatically
Solution
Step 1: Understand the role of try-except
The try-except block is used to catch errors that happen during program execution.
Step 2: Identify the benefit of catching errors
By catching errors, the program can handle them gracefully and continue running instead of crashing.
Final Answer:
To catch and handle specific errors so the program doesn't crash -> Option A
Quick Check:
try-except = catch errors [OK]
Hint: Try-except blocks catch errors to avoid crashes [OK]
Common Mistakes:
Thinking try-except speeds up code
Confusing try-except with comments
Believing try-except creates functions
2. Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
easy
A. try:
x = 1/0
except:
print('Error')
B. try:
x = 1/0
catch ZeroDivisionError:
print('Cannot divide by zero')
C. try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero')
D. try:
x = 1/0
except ZeroDivision:
print('Cannot divide by zero')
Solution
Step 1: Check the correct keyword for catching exceptions
Python uses except to catch exceptions, not catch.
Step 2: Verify the exception name spelling
The correct exception name is ZeroDivisionError, not ZeroDivision.
Final Answer:
try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero') -> Option C
Quick Check:
Use except + exact exception name [OK]
Hint: Use except with exact exception name to catch errors [OK]