GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Python Built-in Exceptions - GeeksforGeeks Python Built-in ExceptionsLast Updated : 18 Apr, 2026 Python provides a set of built-in exceptions, each designed to signal a specific type of error and helps to debug more effectively. These built-in exceptions can be viewed using the locals() built-in functions as follows :
This returns a dictionary of built-in exceptions, functions and attributes. Examples of Built in Exceptions1. BaseExceptionThis class is the root of Python's exception hierarchy. All other exceptions directly or indirectly inherit from it. While it is rarely used directly in code, it is important because it forms the foundation of Python’s error-handling system. Example: This example manually raises a BaseException and catches it to show how the root exception works.
Output This is a BaseException Explanation: Here, we forcefully raise a BaseException. Since we catch it in the except block, message is printed instead of the program crashing. 2. ExceptionException class is the base for all non-exit exceptions. You will often catch Exception in general error-handling code when you are not targeting a specific error type. Example: This code raises a generic Exception and handles it inside the except block.
Output This is a generic exception Explanation: We manually raise an Exception with a message. The error is caught and message is displayed instead of halting the program. 3. ArithmeticErrorArithmeticError class is the base for all errors related to mathematical operations. You don’t usually raise it directly, but it provides a way to catch all math-related errors in one block. Example: This example raises an ArithmeticError manually to demonstrate how it works.
Output Arithmetic error occurred Explanation: We raise an ArithmeticError with a custom message. The program catches it and prints the error message, preventing a crash. 4. ZeroDivisionErrorZeroDivisionError occurs when you attempt to divide a number by zero. Since division by zero is undefined in mathematics, Python raises this exception to signal the error. Example: This code attempts to divide 10 by 0, which triggers a ZeroDivisionError.
Output division by zero Explanation: operation 10/0 is invalid. Python raises a ZeroDivisionError, which is then caught in the except block. Instead of crashing, program prints the error message. 5. OverflowErrorOverflowError occurs when the result of a numerical operation is too large for Python to represent. While it handles large integers well, certain floating-point operations (like very large exponentials) can still cause this error. Example: This example uses the math.exp() function with a very large input, which causes an overflow.
Output math range error Explanation: exponential function with input 1000 produces a number too large to handle. Python raises an OverflowError, which is caught and displayed as an error message. 6. FloatingPointErrorFloatingPointError occurs when a floating-point calculation fails. By default, Python handles most floating-point issues silently (like dividing by zero results in inf or nan). However, you can explicitly enable floating-point error reporting with libraries like NumPy. Example: This example enables error reporting in NumPy and performs a division by zero to trigger FloatingPointError.
Output FloatingPointError caught: divide by zero encountered in divide Explanation: Normally, NumPy would return inf for 1/0. But since we enabled error raising, the division explicitly throws a FloatingPointError, which we catch and print. 7. AssertionErrorAssertionError is raised when the assert statement fails. The assert keyword is often used for debugging or testing assumptions in code. Example: This example checks if 1 == 2 using assert. Since the condition is false, it raises an AssertionError.
Output Assertion failed Explanation: assert statement expects 1 == 2 to be true, but it isn’t. Therefore, Python raises an AssertionError with the message "Assertion failed". 8. AttributeErrorAttributeError occurs when you try to access or assign an attribute that does not exist for an object. Example: This example tries to access a non-existent attribute in a class instance.
Output 'MyClass' object has no attribute 'some_attribute' Explanation: object obj is created from MyClass, which has no attributes. Attempting to access some_attribute raises an AttributeError. 9. IndexErrorIndexError happens when you try to access a list (or any sequence) element with an index that is out of range. Example: This example tries to access the 6th element of a list that only has 3 elements.
Output list index out of range Explanation: list has only indices 0, 1, 2. When we try to access index 5, Python raises an IndexError. 10. KeyErrorKeyError occurs when you try to access a dictionary key that doesn’t exist. Example: This example tries to access the key "key2" in a dictionary that only has "key1".
Output 'key2' Explanation: dictionary does not contain "key2". Attempting to retrieve it raises a KeyError, which is caught and printed. 11. MemoryErrorMemoryError occurs when Python cannot allocate enough memory for an operation. This usually happens when trying to create extremely large data structures. Example: This example tries to create a very large list, which may exceed memory limits.
Explanation: Attempting to create a list with 10**10 elements requires more memory than available. Python raises a MemoryError. 12. NameErrorNameError occurs when you use a variable or function name that has not been defined. Example: This example tries to print a variable that was never declared.
Output name 'var' is not defined Explanation: Since var was never defined in the code, Python raises a NameError. 13. OSError (and related errors)Raised when a system-related operation (like file I/O, opening files, or interacting with the OS) fails. In Python 3:
Example: This example attempts to open a missing file, which triggers FileNotFoundError (a subclass of OSError).
Output FileNotFoundError caught: [Errno 2] No such file or directory: 'non_existent_file.txt' Explanation: missing file raises FileNotFoundError, which is a subclass of OSError. Since we catch it specifically, the error is displayed clearly. More Built-in ExceptionsApart from the exceptions we explored with examples above, Python provides several other built-in exceptions. These usually occur in specific situations. Below is a summary table of these exceptions and what they represent:
Comment Explore | ||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |