Exceptions
3 options:
assert
raise
try
/except
block
Try/Except block:
If you handle exceptions within except
blocks, the program will not end. The exception will be handled, and program execution will continue. Unless you put a raise
statement in the except block.
Python will match exceptions in the order they're listed by type in the try/except block.
Commonly used built-in exception types in Python:
Exception
: The base class for all built-in exceptions.TypeError
: Raised when an operation or function is applied to an object of inappropriate type.ValueError
: Raised when a function receives an argument of the correct type but an invalid value.NameError
: Raised when a local or global name is not found.IndexError
: Raised when a sequence subscript is out of range.KeyError
: Raised when a dictionary key is not found.FileNotFoundError
: Raised when a file or directory is requested but cannot be found.IOError
: Raised when an I/O operation (such as reading or writing a file) fails.ZeroDivisionError
: Raised when a division or modulo operation is performed with a divisor of zero.AttributeError
: Raised when an attribute reference or assignment fails.ImportError
: Raised when an import statement fails to find a module.NotImplementedError
: Raised by abstract methods that should be overridden in subclasses.OverflowError
: Raised when the result of an arithmetic operation is too large to be expressed in the available numeric type.SyntaxError
: Raised when there is a syntax error in the code.IndentationError
: Raised when there is an incorrect indentation in the code.
From Bard:
Here's a guide to when to use assert
, raise
, and try
in Python:
Assert:
Purpose: To verify internal program logic and assumptions during development and testing.
Behavior: If the assertion fails, an
AssertionError
is raised, typically halting execution.Use cases:
Checking preconditions for functions and methods.
Validating data invariants within functions and classes.
Ensuring expected states during testing.
Example:
Python
Raise:
Purpose: To signal explicit errors or exceptional situations that require handling.
Behavior: Raises a specified exception, which can be caught using
try-except
blocks.Use cases:
Custom errors for specific failure conditions.
Signaling errors from external sources (e.g., file I/O, network operations).
Re-raising caught exceptions for further handling.
Example:
Python
Try:
Purpose: To handle potential exceptions and control program flow when errors occur.
Behavior: Executes code within the
try
block, catching specified exceptions inexcept
blocks.Use cases:
Preventing program crashes from unexpected errors.
Implementing alternative actions or error recovery.
Cleaning up resources (e.g., closing files) even when errors occur.
Example:
Python
Key Considerations:
Assertions: Primarily for debugging and testing, not for runtime error handling.
Raising Exceptions: Used to signal errors that need to be addressed.
Try-Except: Catches and handles exceptions for controlled error management.
Choose the appropriate construct based on your specific needs for debugging, error signaling, or error handling in different scenarios.
Last updated