Skip to content
← Computer Science

Computer Science · Class 12 Optional

Ch 1Exception Handling in Python — Class 12 Computer Science, concept-first.

When you write a Python program, you expect it to run smoothly and give the correct output. But sometimes, things go wrong. The program might not run at all, or it might run but produce unexpected results, or it might behave in a strange, abnormal way.

9

Q&A

4

Concepts

Not available

Exam weightage

Start learning — read this chapter →

Key concepts

Hover a concept to preview it and jump to its most relevant Q&A.

Chapter contents

The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.

1.1

Introduction

When you write a Python program, you expect it to run smoothly and give the correct output. But sometimes, things go wrong.

1.2

Syntax Errors

A syntax error is what happens when you break the grammar rules of Python while writing code. The interpreter cannot understand what you have written because it does not follow the language's structur…

1.3

Exceptions

An exception is not a syntax mistake. It is an error that happens during the execution of a program, even when the code is perfectly correct in terms of grammar and spelling.

1.4

Built-in Exceptions

Built-in exceptions are the errors that the Python interpreter already knows about. When you write code, certain mistakes happen so often that the language designers have pre-defined a specific except…

1.5

Raising Exceptions

When a program runs into an error, the Python interpreter automatically raises (or throws) an exception. This is the normal way Python signals that something has gone wrong.

1.5.1

The raise Statement

The raise statement is the tool Python gives you to deliberately throw an exception. You are not waiting for an error to happen — you are actively creating one when your code detects a condition that…

1.5.2

The assert Statement

The assert statement is a debugging aid that tests a condition inside your program. Think of it as a sanity check: you are asserting that something must be true at that point in the code.

1.6

Handling Exceptions

Exception handling is the process of writing additional code in a program to deal with exceptions — those runtime errors that would otherwise crash the program abruptly.

1.6.1

Need for Exception Handling

Exception handling is not unique to Python — it is a technique used across most modern programming languages, including C++ and Java.

1.6.2

Process of Handling Exception

When Python encounters an error during program execution, it does not simply crash. Instead, it creates a special object called the exception object.

1.6.3

Catching Exceptions

An exception is said to be caught when a piece of code specifically designed to handle that exception actually runs.

1.6.4

try...except...else clause

The try...except block can be extended with an optional else clause. This clause is placed after all the except blocks and before the end of the try statement.

1.7

Finally Clause

The try statement can also have an optional finally clause. Statements written inside the finally block always get executed, regardless of whether an exception occurred in the try block or not, and re…

1.7.1

Recovering and continuing with finally clause

If an error is detected in the try block and the exception raised there is handled by a matching except clause, that except block runs as usual.

Summary

- Syntax errors (parsing errors) are detected when the rules of the programming language have not been followed while writing a program.

Exercises