Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-1 Exception Handling in Python

School Revise · Class 11 Computer Science · Chapter 1

Exception Handling in Python

Code 083, Class 12. Programs can meet errors while running. Here we catch these errors with try and except, so the program does not crash.

try

catches errors

finally

always runs

What this chapter is about

A syntax error is a mistake in the way code is written. An exception is an error that happens while the program runs, such as dividing by zero. We handle exceptions so the program keeps going.

1. try and except

Put risky code inside try. If an exception happens, the matching except block runs instead of crashing.

try: print(10 / 0) except ZeroDivisionError: print(“Cannot divide by zero”) Output: Cannot divide by zero

2. else and finally

An else block runs when there was no error. A finally block runs every time, whether or not an error happened, and is used to close files or clean up.

3. Common exceptions and raise

Common exceptions include ZeroDivisionError, ValueError, TypeError, IndexError, KeyError and FileNotFoundError. You can also raise your own exception to signal a problem.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Handle division by zero.

SOLUTION

try: print(10 / 0) except ZeroDivisionError: print(“Cannot divide by zero”)

OUTPUT

Cannot divide by zero
Problem 2. Handle a bad number conversion.

SOLUTION

try: n = int(“abc”) except ValueError: print(“Not a number”)

OUTPUT

Not a number
Problem 3. Use finally to always run a line.

SOLUTION

try: print(“trying”) finally: print(“always runs”)

OUTPUT

trying always runs
Problem 4. Use try, except and else together.

SOLUTION

try: x = 5 except ValueError: print(“error”) else: print(“no error, x is”, x)

OUTPUT

no error, x is 5
Problem 5. Catch an IndexError from a list.

SOLUTION

lst = [1, 2, 3] try: print(lst[5]) except IndexError: print(“no such index”)

OUTPUT

no such index
Problem 6. Catch a KeyError from a dictionary.

SOLUTION

d = {“a”: 1} try: print(d[“b”]) except KeyError: print(“no such key”)

OUTPUT

no such key
Problem 7. Catch two exceptions together.

SOLUTION

try: n = int(“x”) except (ValueError, ZeroDivisionError): print(“caught an error”)

OUTPUT

caught an error
Problem 8. Raise your own exception.

SOLUTION

def check(age): if age < 0: raise ValueError(“age cannot be negative”) return age try: check(-1) except ValueError as e: print(e)

OUTPUT

age cannot be negative
Problem 9. Write a safe division function.

SOLUTION

def safe_div(a, b): try: return a / b except ZeroDivisionError: return “undefined” print(safe_div(10, 2)) print(safe_div(10, 0))

OUTPUT

5.0 undefined
Problem 10. Read a number safely.

SOLUTION

try: n = int(input(“Enter a number: “)) print(“You entered”, n) except ValueError: print(“That was not a number”)

OUTPUT

Enter a number: 7 You entered 7

Practice set A, multiple choice

1. An error that happens while a program runs is called an …

exception.

2. Which block runs whether or not an error happened?

finally.

3. Dividing by zero raises which exception?

ZeroDivisionError.

4. Which keyword signals your own error?

raise.

Quick summary

A syntax error is a writing mistake; an exception happens while running. Risky code goes in try, and a matching except handles the error so the program does not crash. else runs when there is no error, and finally always runs. Common exceptions include ValueError, ZeroDivisionError, IndexError and KeyError, and raise signals your own exception.

Open the Virtual Lab

These free Class 12 Computer Science notes explain exception handling in Python, try except else and finally, common exceptions, and raising your own exception with worked Python programs and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories