School Revise · Class 11 Computer Science · Chapter 1
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.
|
|
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.
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 |
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.
Common exceptions include ZeroDivisionError, ValueError, TypeError, IndexError, KeyError and FileNotFoundError. You can also raise your own exception to signal a problem.
Explore the idea by tapping. The interactive opens right here in the lesson.
Write and run real Python right here in the lesson, then work through the practice problems with answers.
Type each one into the code lab above, then open the card to see the worked solution and its output.
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
exception.
finally.
ZeroDivisionError.
raise.
|
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.