Skip to main content
Chapter 1 of 13
NCERT Solutions

Exception Handling in Python

CBSE · Class 12 · Computer Science

NCERT Solutions for Exception Handling in Python — CBSE Class 12 Computer Science.

45 questions76 flashcards5 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.

1,000+ Class 12 students started this chapter today

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

This is just one of 11+ visuals inside Super Tutor's Exception Handling in Python chapter

Explore the full set
9 Questions Solved · 1 Section

5 worked solutions below. 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.Show solution
- ImportError: Raised when the requested module definition is not found. For example, importing a module that does not exist.
- IOError: Raised when the file specified in a program statement cannot be opened. For example, trying to open a file that is not present.
- NameError: Raised when a local or global variable name is not defined. For example, using a variable before assigning a value to it.
- ZeroDivisionError: Raised when the denominator in a division operation is zero. For 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:

```python
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.

```python
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:Show solution
a) 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.

b) Throwing an exception: Creating an exception object and handing it over to the runtime system when an error occurs; this causes control to jump to an exception handler.

c) Catching an exception: Executing a code block designed to handle a particular exception when that exception is raised. In Python, exceptions are caught in the try block and handled in the except block.

Not sure why a step works? check your working in Super Tutor

6Explain catching exceptions using try and except block.
7Consider the code given below and fill in the blanks.
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.
9What is the use of finally clause? Use finally clause in the problem given in Question No. 7.

4 more solved questions in Exception Handling in Python

Every remaining exercise is solved step by step in Super Tutor, plus practice quizzes and flashcards for this chapter. Free to start.

Stuck on a step?

Ask Super Tutor AI to explain any solution on this page in a simpler way — free, 24x7.

Ask a Doubt Free

Frequently Asked Questions

What are the important topics in Exception Handling in Python for CBSE Class 12 Computer Science?
Exception Handling in Python covers several key topics that are frequently asked in CBSE Class 12 board exams. Focus on the core concepts listed on this page and practise related questions to build confidence.
How to score full marks in Exception Handling in Python — CBSE Class 12 Computer Science?
Understand the core concepts first, then work through the 45 practice questions available for this chapter. Revise formulas and definitions regularly, and use flashcards for quick recall before the exam.
Where can I get free NCERT Solutions for Exception Handling in Python Class 12 Computer Science?
This page has free step-by-step NCERT Solutions for every exercise question in Exception Handling in Python (CBSE Class 12 Computer Science) — written the way examiners award marks: given, formula, working, answer.

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 — for free.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan for CBSE Class 12 Computer Science.