Exception Handling in Python Class 12th Important Questions with Answers Python
Updated on June 1, 2025 | By Learnzy Academy
Q1. "Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.
"Every syntax error is an exception, but not every exception is a syntax error."
Syntax errors happen when the code structure is wrong (e.g., missing :) — they are exceptions raised before execution.
Other exceptions(like ZeroDivisionError, ValueError, etc.) occur during program execution, even if the syntax is correct.
Q2. Name any four common exceptions in Python.
Here are four common exceptions in Python:
- TypeError– Raised when an operation or function is applied to an object of inappropriate type.
Example: len(5) - ValueError– Raised when a function receives an argument of the correct type but inappropriate value.
Example: int('abc') - IndexError – Raised when a sequence subscript is out of range.
Example: my_list[10] where my_list has fewer than 11 items. - KeyError – Raised when a dictionary key is not found.
Example: my_dict['missing_key'] when 'missing_key' does not exist in the dictionary.
Q3. Write the syntax of exception handling in Python.
The basic syntax of exception handling in Python uses the try, except, else, and finally blocks. Here's the general structure is -
try:
# Code that might raise an exception
except ExceptionType:
# Code to handle the exception
else:
# Code that runs if no exception occurs (optional)
finally:
# Code that always runs, whether an exception occurs or not (optional)
Q4. What is the use of the finally block?
The finally block in Python is used to define cleanup actions that must be executed under all circumstances, regardless of whether an exception was raised or not in the try block.
Key Uses of finally:
- Releasing resources (e.g., closing files, database connections, or network sockets)
- Executing important code that must run no matter what (e.g., logging, status updates)
try:
file = open('example.txt', 'r')
data = file.read()
except FileNotFoundError:
print("File not found.")
finally:
file.close()
print("File closed.")
In this example, the file is closed even if an exception occurs while trying to open or read it. This ensures proper resource management.
Q5. How can you catch multiple exceptions in one block?
You can catch more than one type of error using a single except block by putting the exception names in round brackets.
Syntax:-
try:
# Code that may give different types of errors
except (ErrorType1, ErrorType2):
# This block will run if any of the errors happen
Example:--
try:
a = int("abc") # This gives ValueError
b = 10 / 0 # This gives ZeroDivisionError
except (ValueError, ZeroDivisionError):
print("An error occurred!")
Explanation:
- If any of the errors (ValueError or ZeroDivisionError) happens, the except block will run.
- This helps to handle multiple errors together without writing separate except blocks.
Q6. What is the use of raise in Python?
The raise keyword in Python is used to manually trigger an exception (error) in your program.
Why use raise?
- To show a custom error message.
- To stop the program when something goes wrong.
- To make sure the program follows certain rules.
Example:--
age = -5
if age < 0:
raise ValueError("Age cannot be negative")
Q7. What is the purpose of the else block in exception handling?
In Python, the else block in exception handling is used to define code that should run only if no exception was raised in the try block.
Purpose:
- It separates the code that should only run when the try block is successful, making the intent clearer.
- Helps in cleaner exception handling logic by keeping normal execution flow separate from error handling.
Example:--
try:
result = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero.")
else:
print("Division successful. Result is:", result)