Overview
This lecture covers exception handling in Python, including the types of errors, built-in and user-defined exceptions, and the use of try, except, else, and finally blocks to manage errors and prevent program crashes.
Basics of Exceptions in Python
- Exception handling prevents Python programs from crashing due to errors.
- Errors can be syntax errors (grammar mistakes), runtime errors (occur during execution), or logical errors (flaws in logic).
- Syntax errors are detected when code violates Python language rules (e.g., missing parentheses).
- An exception is a Python object representing an error that occurs during execution.
Built-in Exceptions
- Built-in exceptions are predefined in Python (e.g., SyntaxError, ValueError).
- SyntaxError: Raised for code with incorrect grammar.
- ValueError: Raised when an operation receives the right data type but an inappropriate value.
- IOError: Raised when a file can't be opened.
- KeyboardInterrupt: Raised when code execution is interrupted by the user.
- ImportError: Raised when a module can't be found.
- ZeroDivisionError: Raised when dividing by zero.
- IndexError: Raised when accessing an out-of-range index in a sequence.
- NameError: Raised when a variable is not defined.
- TypeError: Raised when an operation is applied to an inappropriate type.
- IndentationError: Raised for incorrect indentation in code.
User-defined Exceptions
- Programmers can create custom exceptions using the
raise statement.
- The
assert statement is used for validating expressions; if false, AssertionError is raised.
- Once an exception is raised, the current code block stops executing and control moves to the exception handler.
Exception Handling Process
- Exception handling involves writing additional code to manage errors gracefully.
- When an error occurs, Python creates an exception object, which is passed to the runtime system.
- The runtime system searches for an appropriate exception handler (in the call stack).
- If no handler is found, the program stops executing.
Syntax of Exception Handling
- Code that may cause exceptions is placed inside a
try block.
- Error handling code is placed in one or more
except blocks.
- The
else block runs if no exception is raised in the try block.
- The
finally block always executes, regardless of exceptions.
Catching Multiple Exceptions
- Multiple
except blocks can handle different exception types after a single try block.
- A generic
except without an exception name catches all exceptions.
- The
else block executes only if no exception occurs.
- The
finally block always runs after try, except, and else blocks.
Key Terms & Definitions
- Exception — An error detected during program execution, represented as a Python object.
- Exception Handling — Techniques to manage errors and prevent program crashes.
- try block — Section of code that may cause exceptions.
- except block — Handles exceptions if raised in the try block.
- else block — Executes if the try block does not raise an exception.
- finally block — Executes regardless of whether an exception occurred.
- raise statement — Used to throw a user-defined exception.
- assert statement — Tests if an expression is true; raises AssertionError if false.
Action Items / Next Steps
- Complete assigned exercise questions on exception handling.
- Review and practice Python code for try, except, else, finally, raise, and assert.
- Study the key differences between syntax, runtime, and logical errors.