Class 12 Computer Science Chapter 1 Revision Summary Strictly NCERT

Revision Summary: Exception Handling in Python (NCERT Class 12)

1. Chapter at a Glance

  • Syntax errors (parsing errors) are detected by the interpreter before execution; the program does not run until they are corrected.
  • An exception is a Python object representing an error that arises during execution even when code is syntactically correct.
  • Built-in exceptions are predefined in Python’s standard library for common errors; the interpreter executes the appropriate handler and displays the exception name.
  • Exceptions can be raised explicitly using the raise statement or tested with the assert statement.
  • Exception handling separates the main program logic from error-detection code by placing suspicious statements in a try block and handler code in except blocks.
  • The runtime system throws an exception object (containing type, file name and location) and searches the call stack to find a matching handler; if none is found, the program terminates.
  • The optional else clause executes only when no exception occurs in the try block; the finally clause always executes regardless of whether an exception occurred.
  • Exception handling allows the program to give meaningful messages and continue instead of terminating abruptly.

2. Key Terms and Definitions

  • Syntax Error / Parsing Error: Error reported by the interpreter when rules of Python are not followed; displayed with name and brief description in both shell and script mode.
  • Exception: A Python object that represents an error raised during program execution; needs to be handled to prevent abnormal termination.
  • Built-in Exception: Predefined exception in the compiler/interpreter for commonly occurring errors (e.g., ZeroDivisionError, IndexError, NameError, TypeError, ValueError, IOError, ImportError, EOFError, KeyboardInterrupt, IndentationError, OverFlowError, SyntaxError).
  • User-defined Exception: Custom exception created by the programmer to suit specific requirements.
  • Raise (Throw) an Exception: Interrupting normal flow of execution and jumping to exception-handler code using raise or assert.
  • Exception Handler: Code written to handle a specific exception when it is raised.
  • Throwing an Exception: Process of creating an exception object (with error details) and handing it to the runtime system.
  • Catching an Exception: Executing the handler code designed for a particular exception.
  • Call Stack: Hierarchical list of methods searched by the runtime system (in reverse order of calls) to locate a suitable exception handler.
  • try Block: Block containing statements where exceptions might occur.
  • except Block: Block containing code to handle one or more specific exceptions raised in the try block.
  • else Clause: Optional clause executed only when no exception is raised in the try block.
  • finally Clause: Optional block whose statements are always executed, whether or not an exception occurred.

3. Syntax and Constructs

raise statement
General form:
python raise exception-name[(optional argument)] Example:
python raise IndexError

assert statement
General form:
python assert Expression[,arguments] Example:
python assert(number >= 0), "OOPS... Negative Number"

try…except block
General form:
python try: # statements that may raise exceptions except ExceptionName: # handler code Example:
python try: q = 50 / denom except ZeroDivisionError: print("Denominator as ZERO.... not allowed")

try…except…else
General form:
python try: ... except ExceptionName: ... else: # executed only if no exception Example:
python else: print("The result of division operation is", quotient)

try…except…finally
General form:
python try: ... except ExceptionName: ... finally: # always executed Example:
python finally: print("OVER AND OUT")

4. Algorithms and Worked Logic

Process of exception handling (as per NCERT flowchart)
1. An error occurs → Python creates an exception object.
2. The object is thrown to the runtime system.
3. Runtime system searches the call stack for a matching except handler.
4. If a handler is found, it is executed (exception is caught).
5. If no handler is found after searching the entire call stack, program execution stops.
6. finally block (if present) executes before control leaves the try structure or before re-raising an unhandled exception.

Handling multiple exceptions

  • Write one try block followed by multiple except blocks.
  • When an exception is raised, Python searches the except blocks in order until a match is found.
  • An unnamed except: clause (placed last) can catch any remaining unhandled exception.

5. Common Errors and Exam Pitfalls

  • Placing code that must always run (e.g., file close) before the finally clause or forgetting that finally executes even when an exception is re-raised.
  • Using else without a preceding except or expecting else to run when an exception occurs.
  • Writing an unnamed except: before a named except clause (Python requires named handlers first).
  • Forgetting that after finally executes, an unhandled exception is re-raised and may terminate the program.
  • Assuming SyntaxError behaves like other exceptions—it is raised before execution and cannot be caught at runtime in the same way.
  • Not handling all possible exceptions that can arise from a single try block when the question asks for “appropriate exception” handling.

A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.