Notes · Class 12 · CBSE · Science Computer Science

Exception Handling in Python notes

Revision notes for the chapter: the key points, definitions and exam tips to remember.

Exceptions are errors that occur while a program runs. Handling them prevents crashes.

ERRORS

  • Syntax errors: detected before running (missing colon, wrong indentation).
  • Exceptions: runtime errors.

BUILT-IN EXCEPTIONS

  • SyntaxError, ValueError (int("abc")), IOError, KeyboardInterrupt, ImportError, EOFError, ZeroDivisionError, IndexError, NameError, IndentationError, TypeError, OverflowError.

RAISING EXCEPTIONS

  • raise ValueError("message").
  • assert condition, "message" — raises AssertionError if false.

HANDLING

  • try: code that might fail
  • except ExceptionType: handler (can have several except blocks; a bare except catches all)
  • else: runs if no exception occurred
  • finally: always runs (closing files, releasing resources)

FLOW

  • Python searches the call stack for a matching handler; if none is found, the program stops with a traceback.
Memory hook: "try the risky part, except the error, else celebrate, finally clean up."

Exception Handling in Python: practice questionsException Handling in Python cheat sheet

More Class 12 · CBSE · Science Computer Science notes

Practise Exception Handling in Python

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

Start practising free

Free to start · No credit card needed