KwickAcademy Python · 7 min · free
Exception Handling: try, except, else and finally
Learn Python exception handling for CBSE Class 12: errors vs exceptions, try, except, else, finally, specific exceptions, raise. An exception is an error that happens while the program is running; if nobody handles it, the program crashes.
Follows the syllabus of: CBSE Class 12 Computer Science (083)
On screen in this lesson
Errors and exceptions
| Syntax error: code breaks a rule, nothing runs |
| Exception: an error found while the program runs |
| An unhandled exception crashes the program |
| Python prints a traceback with the error name |
Code that crashes
| Code | Exception | Why |
|---|---|---|
| 1000 / 0 | ZeroDivisionError | divide by zero |
| int("ten") | ValueError | not digits |
| "Rs " + 50 | TypeError | text plus number |
| print(total) | NameError | total not made |
Rules for the four blocks
| Order: try, except, else, finally |
| try needs at least one except or a finally |
| else runs only when try had no exception |
| finally always runs, error or not |
Why be specific?
| A bare except catches every exception |
| It also hides your own typing mistakes |
| Catch only the errors you expect |
| Put the most specific except first |
Built-in exception types
| Exception | Happens when | Example |
|---|---|---|
| ZeroDivisionError | dividing by zero | 5 / 0 |
| ValueError | bad value | int("abc") |
| TypeError | wrong type | "5" + 2 |
| IndexError | position too big | [1, 2][5] |
| KeyError | key not in dict | {}["roll"] |
| NameError | name not defined | print(marks) |
Quick recap
| An exception is an error while the program runs |
| try holds risky code; except handles the error |
| else runs if no error; finally always runs |
| Catch specific exceptions, not every exception |
| raise creates an exception on purpose |
Quick answers
When does the else block run?
Only when the try block finished with no exception.
Does finally run if there is an error?
Yes. finally always runs.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What is an exception?42 sec
Does else run when there is an exception?44 sec
Why avoid a bare except?39 sec
What does raise do?41 secThe full lesson, in text
Hello students, welcome to Kwickprep. A bill splitting app crashes when someone types zero friends. Can we stop that crash? Yes. Today we will learn exceptions, the try, except, else and finally blocks, catching specific errors, and the raise statement.
First, two kinds of errors. A syntax error means the code breaks a writing rule, like a missing colon, so nothing runs at all. An exception is an error that happens while the program is running, such as dividing by zero. If nobody handles the exception, the program stops at once, which we call a crash. Python then prints a traceback, a message that shows the line and the error name.
Here are four lines that crash, each with its exception name. Dividing a thousand rupees by zero friends gives a zero division error. Int of the word ten gives a value error, because the text has no digits. Adding text and a number gives a type error. Printing a variable that was never created gives a name error.
To handle an exception, put the risky code inside a try block. Below it, an except block says what to do if a certain exception happens. Here, friends is zero, so the division raises a zero division error. Python jumps straight to the except block and prints a friendly message. The program does not crash, so the last line also runs.
Here are all four blocks drawn as paths. First, Python runs the try block. The diamond asks, was an exception raised? If yes, Python goes to the side and runs the matching except block, and else is skipped. If no, the flow goes down and runs the else block. On both paths, the finally block runs at the end, and then the program moves on.
Now the full pattern in code. A short block may sit on the same line, after the colon. Friends is four, so the try block works and no exception is raised. So Python skips except and runs else, which prints each pays two hundred fifty point zero. A single slash always gives a float, even when the answer is whole. Then finally prints Done. Pause and predict. What prints if friends is zero?
Now friends is zero, so the try block raises a zero division error. The except block prints Cannot split. The else block is skipped, because an exception happened. But finally still prints Done, because finally always runs.
Remember four rules. The blocks always come in this order: try, except, else, then finally. A try must have at least one except block or a finally block. The else block runs only when the try block finished with no exception. The finally block runs every time, so it is used for clean up work, such as closing a file.
One try can have many except blocks, one for each kind of problem. Python checks them from top to bottom and runs only the first one that matches. Here the text twelve a has a letter, so int raises a value error, and we see Not a number. If the text were zero instead, the division would fail, and we would see Zero not allowed.
One except can catch several exceptions, if you write their names in round brackets. The word as stores the exception in a variable, here called e, so we can print its message. The list has three marks, at positions zero, one and two. Position three does not exist, so Python raises an index error with this message.
You may also see except with no name after it, called a bare except. It catches every exception, even ones you did not expect. So it can hide your own bugs, like a misspelt variable name. Good programs catch only the exceptions they expect and can fix. And if you list several excepts, put the most specific one first.
Python has many built-in exception types, and exams often ask you to name them. Zero division error comes from dividing by zero. Value error means the type is right but the value is wrong, such as int of some letters. Type error means the type is wrong, such as adding text and a number. Index error means a list position does not exist. Key error means a dictionary has no such key. Name error means a variable was never created.
Sometimes Python sees no problem, but your program knows a value is wrong. An age of minus five makes no sense. The raise statement creates an exception on purpose, with a message. Here, less than zero is True, so raise sends a value error with the message Bad age, and except prints it. Pause and predict. Is an age of exactly zero an error? No, zero is not less than zero, so it is returned.
Here is a small project using everything together. The recharge function raises a value error if the amount is zero or less. A for loop tries two amounts, one after another. One hundred ninety nine rupees is fine, so it prints the message. Zero raises the exception, and except prints Invalid, so the program keeps going.
Let us revise what we learned today. An exception is an error that happens while the program runs, and unhandled it crashes the program. Try holds the risky code, and except handles the error. Else runs only when there was no error, and finally always runs. Catch specific exceptions by name, not every exception. And raise creates an exception on purpose, when a value is wrong.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Computational Thinking and Programming – 2 |
| Programming All levels Python | Errors and Exception Handling |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

