PYTHON EXCEPTION HANDLING
Exam Power Notes • Quick Revision Guide
Description: Concise study notes covering Python exception handling, common keywords, examples, exam-focused
questions, and MCQs for quick revision.
Keywords: Python, Exception Handling, try, except, else, finally, raise, custom exceptions, errors, debugging,
programming exam, quick revision
1. What is Exception Handling?
Exception handling is a mechanism used to deal with runtime problems without abruptly terminating a program.
Python commonly uses try, except, else, and finally blocks.
2. Important Keywords
Keyword Purpose
try Contains code that may produce an exception.
except Handles a matching exception.
else Runs when the try block finishes without an exception.
finally Runs whether an exception occurs or not.
raise Manually triggers an exception.
3. Basic Structure
try:
# risky code
except ValueError:
# handle the error
else:
# runs if no exception
finally:
# cleanup code
4. Common Python Exceptions
• ValueError: correct data type but invalid value.
• TypeError: operation/function receives an inappropriate type.
• ZeroDivisionError: division by zero.
• IndexError: sequence index is outside its valid range.
• KeyError: requested dictionary key does not exist.
• FileNotFoundError: requested file cannot be found.
5. Exam-Focused Example
try:
number = int(input('Enter a number: '))
result = 10 / number
except ValueError:
print('Enter a valid integer')
Exam Power Notes • Quick Revision Guide
Description: Concise study notes covering Python exception handling, common keywords, examples, exam-focused
questions, and MCQs for quick revision.
Keywords: Python, Exception Handling, try, except, else, finally, raise, custom exceptions, errors, debugging,
programming exam, quick revision
1. What is Exception Handling?
Exception handling is a mechanism used to deal with runtime problems without abruptly terminating a program.
Python commonly uses try, except, else, and finally blocks.
2. Important Keywords
Keyword Purpose
try Contains code that may produce an exception.
except Handles a matching exception.
else Runs when the try block finishes without an exception.
finally Runs whether an exception occurs or not.
raise Manually triggers an exception.
3. Basic Structure
try:
# risky code
except ValueError:
# handle the error
else:
# runs if no exception
finally:
# cleanup code
4. Common Python Exceptions
• ValueError: correct data type but invalid value.
• TypeError: operation/function receives an inappropriate type.
• ZeroDivisionError: division by zero.
• IndexError: sequence index is outside its valid range.
• KeyError: requested dictionary key does not exist.
• FileNotFoundError: requested file cannot be found.
5. Exam-Focused Example
try:
number = int(input('Enter a number: '))
result = 10 / number
except ValueError:
print('Enter a valid integer')