Cheat sheet · Class 12 · CBSE · Science Computer Science

Exception Handling in Python cheat sheet

The whole chapter on one page: key formulas, facts and the points to remember, for quick revision before a test.

CBSE Class 12 · Computer Science

Cheat sheet

Chapter 3Exception Handling in Python

try → except → else → finally.

1. Errors

  • Syntax errors: caught before running.
  • Exceptions: errors during execution.

2. Built-in Exceptions

ExceptionWhen
ZeroDivisionErrordivision by zero
ValueErrorright type, wrong value (int('abc'))
TypeErrorwrong type for an operation
NameErrorundefined variable
IndexErrorindex out of range
KeyErrormissing dictionary key
IOError / FileNotFoundErrorfile problems
EOFErrorinput() hits end of file

3. Handling

try:
    x = int(input())
    print(10 / x)
except ZeroDivisionError:
    print('Zero!')
except ValueError:
    print('Not a number')
else:
    print('OK')
finally:
    print('Done')

4. Raising

  • raise ValueError('message') throws an exception.
  • assert condition, 'message' checks a condition.

5. Key Points

  • else runs only if no exception.
  • finally always runs.
  • A bare except catches everything.
  • Exceptions propagate up the call stack.

Remember: try/except/else/finally | common exceptions | raise & assert

Exception Handling in Python: questions and notes

More Class 12 · CBSE · Science Computer Science cheat sheets

Practise Exception Handling in Python

Sign up free to practise the full chapter. Questions you get wrong come back until you've mastered them.

Start practising free

Free to start · No credit card needed