Revision Summary: Exception Handling in Python (NCERT Class 12)
raise statement or tested with the assert statement.try block and handler code in except blocks.else clause executes only when no exception occurs in the try block; the finally clause always executes regardless of whether an exception occurred.ZeroDivisionError, IndexError, NameError, TypeError, ValueError, IOError, ImportError, EOFError, KeyboardInterrupt, IndentationError, OverFlowError, SyntaxError).raise or assert.try block.try block.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")
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.
try block followed by multiple except blocks. except blocks in order until a match is found. except: clause (placed last) can catch any remaining unhandled exception.finally clause or forgetting that finally executes even when an exception is re-raised.else without a preceding except or expecting else to run when an exception occurs.except: before a named except clause (Python requires named handlers first).finally executes, an unhandled exception is re-raised and may terminate the program.SyntaxError behaves like other exceptions—it is raised before execution and cannot be caught at runtime in the same way.try block when the question asks for “appropriate exception” handling.A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.