Python exception types refer to the predefined categories or classes of exceptions that can occur during the execution of a Python program. These exception types are used to identify and handle different types of errors and exceptional situations that may arise in your code. Some commonly used Python exception types include:
Here’s a tabular format of some commonly used Python exception types:
Exception Type | Description |
---|---|
Exception | Base class for all built-in exceptions. |
SyntaxError | Raised when there is a syntax error in the code. |
TypeError | Raised when an operation or function is applied to an inappropriate type. |
NameError | Raised when a local or global name is not found. |
FileNotFoundError | Raised when a file or directory is requested, but it cannot be found. |
ValueError | Raised when a function receives an argument of the correct type but an invalid value. |
IndexError | Raised when a sequence subscript is out of range. |
KeyError | Raised when a dictionary key is not found. |
ZeroDivisionError | Raised when the second operand of a division or modulo operation is zero. |
AssertionError | Raised when an assert statement fails. |
These are just a few examples of the many exception types available in Python. You can also define your custom exception types by creating a new class that inherits from the Exception
base class or any other built-in exception class.
Python exception types examples
Here’s an example that demonstrates different Python exception types and how they can be handled:
try:
# Code that may raise exceptions
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = num1 / num2
print("Result:", result)
except ValueError:
print("Error: Invalid input. Please enter a valid number.")
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except NameError:
print("Error: Variable is not defined.")
except Exception as e:
print("Error:", str(e))
else:
print("No exceptions occurred.")
finally:
print("Exception handling complete.")
Output:
In this example, we have multiple exception types being handled:
ValueError
: If the user enters a non-numeric value, aValueError
will be raised when converting the input to an integer.ZeroDivisionError
: If the user enters0
as the second number, aZeroDivisionError
will be raised when dividingnum1
bynum2
.NameError
: The lineprint(undefined_variable)
will raise aNameError
becauseundefined_variable
is not defined.Exception
(catch-all): If any other type of exception occurs, it will be caught by the genericexcept Exception
block, which will print the error message.
The else
block is executed if no exceptions occur, and the finally
block is always executed, regardless of whether an exception occurred or not.
Note: Remember to handle exceptions appropriately based on your specific requirements and provide meaningful error messages to users.
Comment if you have any doubts or suggestions on this Python exception-handling topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.