On this page
Exception handling questions in the CBSE paper usually show a short program and ask what it prints. To answer them you need the exact order in which try, except, else and finally run, and which exception a line will raise. This post covers what the syllabus lists, traces one program through every path, explains raise and assert, and lists the traps. All outputs were checked by running the code in Python 3.
What does the syllabus include?
The CBSE Computer Science curriculum for Class XII (2026-27) lists "Exception Handling: Introduction, handling exceptions using try-except-finally blocks" in Unit 1, which carries 40 of the 70 theory marks. The first chapter of the Class 12 NCERT book, Exception Handling in Python, covers syntax errors and exceptions, a table of built-in exceptions, the raise and assert statements, try with one or more except clauses, the else clause and the finally clause.
What is the difference between a syntax error and an exception?
A syntax error means Python cannot even read the program, so nothing runs. An exception happens while a correct program runs: dividing by zero, opening a missing file, or using an index that does not exist. NCERT's table of built-in exceptions is worth knowing by heart, especially these:
ZeroDivisionError: the denominator in a division is zero.
ValueError: right type, wrong value, as in int("12a").
IndexError: an index outside the sequence.
KeyError: a dictionary key that is not there.
NameError: a variable that was never defined.
TypeError: an operation on the wrong type, as in "5" + 2.
NCERT also lists IOError for a file that cannot be opened. In Python 3, the built-in exceptions page explains that IOError is now another name for OSError, and a missing file raises FileNotFoundError, which except IOError still catches.
In what order do try, except, else and finally run?
def average(marks, n):
try:
avg = sum(marks) / n
print("first mark:", marks[0])
except ZeroDivisionError:
print("n cannot be 0")
avg = None
except IndexError:
print("list is empty")
avg = None
else:
print("no error")
finally:
print("checked")
return avgTrace all three paths:
average([60, 80], 2) prints first mark: 60, no error, checked, and returns 70.0.
average([60, 80], 0) fails on the division. The print in try is skipped. It prints n cannot be 0, then checked, and returns None.
average([], 3) divides 0 by 3 without trouble, then fails on marks[0]. It prints list is empty, then checked, and returns None.
The Python tutorial on errors puts the rules plainly. If an exception occurs in the try clause, "the rest of the clause is skipped". The else clause is "useful for code that must be executed if the try clause does not raise an exception". And "the finally clause runs whether or not the try statement produces an exception".
What happens with return inside try?
finally still runs before the function hands back its value.
def f():
try:
return "try"
finally:
print("finally runs")
print(f()) # finally runs, then tryIf finally has its own return, that value wins. The tutorial notes this "can be confusing and is therefore discouraged", and newer Python versions give a warning for it, so do not write it in your own code.
How do raise and assert work?
You can raise an exception yourself when input is wrong:
def check_age(age):
if age < 0:
raise ValueError("age cannot be negative")
return age
try:
check_age(-4)
except ValueError as e:
print(e) # age cannot be negativeassert is a short check: assert 5 > 7, "five is not greater" raises AssertionError with that message. NCERT uses it to reject negative numbers at the start of a function.
Why does the order of except clauses matter?
Python uses the first except that matches. Exception matches almost everything, so putting it first hides the specific handlers:
try:
x = int("12a")
except Exception:
print("general")
except ValueError:
print("value")This prints general, and the ValueError handler never runs. NCERT's rule is the same for a bare except: with no exception named, it must be the last clause. Put specific exceptions first.
What mistakes cost marks?
Printing lines after the error inside try. They are skipped.
Running else after an exception. It runs only when try succeeds.
Forgetting finally in an output answer, or putting it in the wrong place.
A general except before a specific one.
Naming the wrong exception, such as ValueError for a missing dictionary key, which is KeyError.
The 2026-27 CBSE sample question paper includes a function that writes to a log file inside an except ZeroDivisionError block and asks what the file will contain.
A practice routine for exceptions
The most useful study methods in a large review by Dunlosky and colleagues were practice testing and spacing practice out, so test yourself on short bursts over several days.
Day 1: Write one line that raises each exception in the list above. Run them and read the error names.
Day 2: Copy the average function, predict all three traces on paper, then run it.
Day 4: Add try and except to two of your practical-file programs, for example around input() and file opening.
Day 7: Do the exception question from a sample paper under time.
When a whole topic needs rebuilding, Fermi's AI tutor works out what you already know and builds a plan for the session. Exceptions often appear with files, so pair this with our guide to file handling in Python.
Now type the average function and run it with your own three inputs before checking against the traces above.



