Skip to main content
Chapter 1 of 13
NCERT Solutions

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.

45 questions76 flashcards2 formulas & key relations5 concepts

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.

An infographic comparing Syntax Errors, Runtime Errors (Exceptions), and Logical Errors in Python, highlighting their characteristics, detection time, and impact on program execution.
Super Tutor

Learn better with visuals Super Tutor pairs illustrations like this with notes and quizzes for Exception Handling in Python.

Get started
9 Questions Solved · 1 Section

The 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
  • ImportError is raised when the requested module definition is not found. Example: trying to import a 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
  • 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

6Explain catching exceptions using try and except block.

Free with a Super Tutor account

7Consider the code given below and fill in the blanks.

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

8You have learnt how to use math module in Class XI. Write a code where you use the wrong number of arguments for a method (say sqrt() or pow()). Use the exception handling process to catch the ValueError exception.

Free with a Super Tutor account

9What is the use of finally clause? Use finally clause in the problem given in Question No. 7.

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?
Key topics in Exception Handling in Python include Core Ideas, Built-in Exceptions, Raising Exceptions, Handling Exceptions. Study these first, then practise questions on each for the CBSE Class 12 board exam.
Are these NCERT Solutions for Exception Handling in Python free?
The first 5 of the 9 solutions on this page are open to read. The other 4 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise Exception Handling in Python for the CBSE Class 12 board exam?
Learn the core ideas first, then work through the 45 practice questions on Exception Handling in Python. Revise definitions regularly and use flashcards for quick recall before the exam.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

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.