Skip to content
6 MIN LESSON · COMPUTER SCIENCE

Class 12 Exception Handling: Complete Concepts and Practice

Computer Science — Syntax errors versus exceptions, Python's common built-in exceptions and what triggers each, handling exceptions with try, except, else and finally, multiple except blocks, raising exceptions with raise and assert, and the flow of control when an exception occurs, all with tested examples.

Big picture

Even a correct program can fail at run time: a user types letters instead of a number, a file is missing, or a value is divided by zero. Such run-time errors are exceptions. If an exception is not handled, Python stops the program and prints an error message (a traceback).

Exception handling lets a program catch these errors and respond sensibly: show a helpful message, ask again, or clean up. In Python this is done with try, except, else and finally. Board questions ask you to name the exception a statement raises, write handling code, and predict the output when an exception occurs.

Learning goals

  • Distinguish syntax errors from exceptions.

  • Name common built-in exceptions and the situations that raise them.

  • Use try and except, including multiple except blocks.

  • Use else and finally correctly, and trace the flow of control.

  • Raise exceptions with raise and check conditions with assert.

Core ideas

1. Syntax errors and exceptions

Basis

Syntax error

Exception

When it is detected

Before the program runs (while parsing)

While the program is running

Cause

Code breaks Python's grammar rules

A valid statement can't be carried out

Example

Missing colon: if x > 5 print(x)

10 / 0 raises ZeroDivisionError

Can it be handled with try–except?

No, the program can't start

Yes

SyntaxError and IndentationError are reported before execution; all others in this chapter occur at run time.

2. Common built-in exceptions

Exception

Raised when

Example

ZeroDivisionError

Dividing or taking a remainder by zero

5 / 0

ValueError

A function gets an argument of the right type but an inappropriate value

int('abc')

TypeError

An operation is applied to an object of the wrong type

'2' + 2

NameError

A name that hasn't been defined is used

print(total) before total is assigned

IndexError

A sequence index is out of range

[1, 2][5]

KeyError

A dictionary key doesn't exist

{}['k']

FileNotFoundError (an OSError/IOError)

A file to be opened doesn't exist

open('missing.txt')

ImportError / ModuleNotFoundError

A module or name can't be imported

import maths

EOFError

input() hits end-of-file without reading data

KeyboardInterrupt

The user presses Ctrl+C during execution

OverflowError

A calculation result is too large to represent

math.exp(1000)

IndentationError

Incorrect indentation

3. try and except

Code that might raise an exception goes in a try block. If an exception occurs, Python skips the rest of the try block and jumps to the matching except block.

for v in ['5', 'x']:
    try:
        n = int(v)
        print(10 / n)
    except ValueError:
        print('VE')
    except ZeroDivisionError:
        print('ZE')
# output:
# 2.0
# VE
  • Multiple except blocks handle different exceptions differently. Python checks them in order and runs only the first matching block.

  • except (ValueError, TypeError): handles several exceptions in one block.

  • except ValueError as e: stores the exception object, so its message can be printed: for [1, 2][5] the message is list index out of range.

  • A bare except: catches every exception; place it last, and use it with care because it can hide bugs.

4. else and finally

  • The `else` block runs only if no exception occurred in the try block.

  • The `finally` block always runs, whether or not an exception occurred, and even if the try block contains a return. It is used for clean-up, such as closing files.

def div(a, b):
    try:
        r = a / b
    except ZeroDivisionError:
        print('Cannot divide by zero', end='; ')
        return None
    else:
        print('Result', r, end='; ')
        return r
    finally:
        print('Done')

div(10, 2)    # Result 5.0; Done
div(1, 0)     # Cannot divide by zero; Done

Order of blocks: try → except (one or more) → else (optional) → finally (optional). else can't appear without at least one except.

finally even runs before a return:

def t():
    try:
        return 1
    finally:
        print('F', end=' ')

print(t())    # F 1

5. Flow of control when an exception occurs

  1. Python executes the try block statement by statement.

  2. If an exception occurs, the remaining statements of the try block are skipped.

  3. Python looks for a matching except block. If found, it runs; if not, the exception passes up to the caller, and if no handler exists anywhere, the program stops with a traceback.

  4. If no exception occurs, the else block runs.

  5. The finally block runs in every case.

6. Raising exceptions: raise and assert

`raise` lets a program signal an error deliberately, for example when an input is invalid:

def age_check(a):
    if a < 0:
        raise ValueError('Age cannot be negative')
    return a

try:
    age_check(-3)
except ValueError as e:
    print(e)          # Age cannot be negative

`assert` tests a condition and raises an AssertionError (with an optional message) if it is False. It is mainly used while debugging to check assumptions:

try:
    assert 5 > 10, 'five is not greater'
except AssertionError as e:
    print('AE', e)    # AE five is not greater

7. Why handle exceptions?

  • The program doesn't crash; it can show user-friendly messages and recover.

  • Error-handling code is separated from normal code, so programs are clearer.

  • Resources such as files and database connections can be released in finally.

A reliable routine for exception questions

  1. Identify which statement fails first and the exact exception it raises.

  2. Skip the rest of the try block after that statement.

  3. Find the first except that matches; if none matches, the exception propagates.

  4. Run else only if nothing failed; always run finally.

  5. Write the outputs in order, paying attention to end= in print statements.

Connections that matter

  • File handling uses try–except for FileNotFoundError and finally (or with) to close files.

  • Python–SQL connectivity handles database errors and closes connections.

  • Functions can raise exceptions that are handled by the caller.

Common mistakes

  • Expecting `try`–`except` to catch a syntax error.

  • Putting a general `except` before specific ones, so the specific blocks never run.

  • Thinking `else` runs after an exception. It runs only when there was no exception.

  • Thinking `finally` is skipped when there is a `return`. It still runs.

  • Mixing up `ValueError` and `TypeError`: int('abc') is ValueError; '2' + 2 is TypeError.

Revision checklist

  • I can distinguish syntax errors from exceptions.

  • I can name the exception raised by common statements.

  • I can write try–except with multiple handlers.

  • I can use else and finally and trace their order.

  • I can use raise and assert.

Practise this topic →

Discussion

Sign in to join the discussion.

Sign in

No comments yet. Be the first to share your thoughts.