0% completed
Python provides a number of built-in exceptions that handle different error cases. Understanding these exceptions is crucial for effective error handling and robust Python programming. Below is a table of some common built-in exceptions with rephrased descriptions, followed by examples demonstrating how to handle a couple of these exceptions.
# | Exception | Description |
---|---|---|
1 | BaseException | The superclass for all exceptions, serving as the base class for all other exceptions. |
2 | StopIteration | Triggered when an iterator's next() method signals that there are no further items. |
3 | SystemExit | Triggered by calling sys.exit(), signaling the interpreter to exit. |
4 | StandardError | Obsolete in Python 3, it was the base class for all built-in exceptions except StopIteration and SystemExit. |
5 | ArithmeticError | Superclass for exceptions that occur for errors in numeric calculations. |
6 | OverflowError | Occurs when a calculation exceeds a numeric type's designated limit. |
7 | FloatingPointError | Occurs when a floating point operation fails. |
8 | ZeroDivisionError | Occurs when division or modulo operation is performed with zero as the divisor. |
9 | AssertionError | Triggered when an assert statement fails. |
10 | AttributeError | Occurs when attribute reference or assignment fails. |
11 | EOFError | Triggered when no input is detected from input() or raw_input() and EOF is reached. |
12 | ImportError | Occurs when an import statement fails to find the module definition or cannot load it. |
13 | KeyboardInterrupt | Triggered when the user interrupts program execution, typically with a Ctrl+C. |
14 | LookupError | Base class for errors that occur during a lookup. |
15 | IndexError | Triggered when an index is not found in a sequence. |
16 | KeyError | Occurs when a dictionary key is not found during lookup. |
17 | NameError | Triggered when a local or global name is not found. |
18 | UnboundLocalError | Occurs when a reference is made to a local variable in a function or method but no value has been assigned to it. |
19 | EnvironmentError | Obsolete in Python 3; use OSError instead. Base class for I/O related errors. |
20 | IOError | Obsolete in Python 3; integrated into OSError. Triggered for errors related to I/O operations. |
21 | OSError | Errors related to system operations, encompasses the OS-related errors. |
22 | SyntaxError | Triggered by parser encountering a syntax error. |
23 | IndentationError | A subclass of SyntaxError, triggered by incorrect indentation. |
24 | SystemError | Raised when the interpreter detects an internal error. |
25 | SystemExit | Raised when sys.exit() is called. |
26 | TypeError | Raised when an operation or function is applied to an object of inappropriate type. |
27 | ValueError | Raised when a function receives an argument of correct type but inappropriate value. |
28 | RuntimeError | An error that doesn't fall into any other category, typically used for general runtime errors. |
29 | NotImplementedError | Raised to indicate that an abstract method requires an overridden implementation in a subclass. |
This example demonstrates handling a KeyError
, which occurs when a requested key is not found in a dictionary.
Explanation:
my_dict
only contains keys for 'name'
and 'age'
.my_dict['address']
raises a KeyError
because the 'address'
key does not exist.except KeyError
block catches this specific error and prints a message indicating the key is missing.This example shows how to handle an IndexError
, which is raised when attempting to access an index that is out of the range of a list.
Explanation:
my_list
is defined with three elements: indexes 0, 1, and 2.my_list[5]
raises an IndexError
because there is no element at index 5.except IndexError
block is used to catch the error and inform the user that they have tried to access a non-existent list index.Understanding and properly handling these exceptions are vital for developing dependable and user-friendly Python applications. This approach ensures your program can gracefully handle errors and provide informative feedback to users, improving both usability and reliability.
.....
.....
.....