Exception Handling in Python — NCERT Solutions
CBSE · Class 12 · Computer Science
NCERT question answers for Exception Handling in Python, CBSE Class 12 Computer Science: 9 textbook questions solved step by step. Covers Exercise.
Interactive on Super Tutor
Studying Exception Handling in Python? Get the full interactive chapter.
Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.
Free trial, no card needed.

Learn better with visuals Super Tutor pairs illustrations like this with notes and quizzes for Exception Handling in Python.
Get startedThe first 5 solutions are open to read. The other 4 are free with a Super Tutor account. Unlock all 9 free in Super Tutor
Exercise
1“Every syntax error is an exception but every exception cannot be a syntax error.” Justify the statement.Show solution
A syntax error is detected when the rules of the programming language are not followed while writing the program. Such errors are also called parsing errors. The text says that syntax errors are also handled as exceptions, so every syntax error is an exception.
But many exceptions occur after the program has started executing, even when the program is syntactically correct. Examples are ZeroDivisionError when dividing by zero, NameError when a variable is not defined, and IOError when a file cannot be opened. These are exceptions, but not syntax errors. So, every exception cannot be a syntax error.
Not sure why a step works? check your working in Super Tutor
2When are the following built-in exceptions raised? Give examples to support your answers.a) ImportError
b) IOError
c) NameError
d) ZeroDivisionError
Show solution
a) ImportError
b) IOError
c) NameError
d) ZeroDivisionError
- ImportError is raised when the requested module definition is not found. Example: trying to
importa module that does not exist. - IOError is raised when the file specified in a program statement cannot be opened. Example: trying to open a file with a wrong name or from a wrong location.
- NameError is raised when a local or global variable name is not defined. Example: using a variable before assigning any value to it.
- ZeroDivisionError is raised when the denominator in a division operation is zero. Example:
10/0.
Not sure why a step works? check your working in Super Tutor
3What is the use of a raise statement? Write a code to accept two numbers and display the quotient. Appropriate exception should be raised if the user enters the second number (denominator) as zero (0).Show solution
The raise statement is used to forcefully throw an exception in a program. Once an exception is raised, the normal flow of execution stops and control moves to the exception handler.
Example code to accept two numbers and display the quotient, raising an exception if the denominator is zero:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num2 == 0:
raise ZeroDivisionError("Denominator cannot be zero")
else:
quotient = num1 / num2
print("Quotient =", quotient)If the second number is 0, the program raises ZeroDivisionError and does not continue with the division.
Not sure why a step works? check your working in Super Tutor
4Use assert statement in Question No. 3 to test the division expression in the program.Show solution
Using assert statement, we can test whether the denominator is valid before division. If the condition is false, an AssertionError is raised.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
assert(num2 != 0), "Denominator should not be zero"
quotient = num1 / num2
print("Quotient =", quotient)Here, assert(num2 != 0) checks the division expression indirectly by ensuring the denominator is not zero. If num2 is 0, the message "Denominator should not be zero" is displayed and the program stops.
Not sure why a step works? check your working in Super Tutor
5Define the following:a) Exception Handling
b) Throwing an exception
c) Catching an exception
Show solution
a) Exception Handling
b) Throwing an exception
c) Catching an exception
- Exception Handling: the process of writing additional code to give proper messages or instructions to the user when an exception occurs, so that the program does not crash abruptly.
- Throwing an exception: creating an exception object and handing it over to the runtime system when an error occurs; this interrupts the normal flow of execution.
- Catching an exception: executing a suitable handler code for the raised exception, usually in the except block.
Not sure why a step works? check your working in Super Tutor
Free with a Super Tutor account
print (" Learning Exceptions...")
try:
num1= int(input ("Enter the first number"))
num2=int(input("Enter the second number"))
quotient=(num1/num2)
print ("Both the numbers entered were correct")
except __________: # to enter only integers
print (" Please enter only numbers")
except __________: # Denominator should not be zero
print(" Number 2 should not be zero")
else:
print(" Great .. you are a good programmer")
__________: # to be executed at the end
print(" JOB OVER... GO GET SOME REST")
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
4 more solved questions in Exception Handling in Python
They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.
Frequently Asked Questions
What are the important topics in Exception Handling in Python for CBSE Class 12 Computer Science?
Are these NCERT Solutions for Exception Handling in Python free?
How should I revise Exception Handling in Python for the CBSE Class 12 board exam?
Sources & Official References
- NCERT Official — ncert.nic.in
- CBSE Academic — cbseacademic.nic.in
- CBSE Official — cbse.gov.in
- National Education Policy 2020 — education.gov.in
Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.
More resources for Exception Handling in Python
Practice Quiz
Test yourself with a quick quiz
Important Questions
Exam-style questions with answers
Revision Notes
Key points for last-minute revision
Formula Sheet
The chapter's formulas in one place
Chapter Summary
Understand the chapter at a glance
Concept Maps
See how topics connect
Study Plan
Step-by-step plan for this chapter
Flashcards
Quick-fire cards for active recall
Syllabus
What topics to cover
For serious students
Get the full Exception Handling in Python chapter — start free.
Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 12 Computer Science. Free to start, no card needed.