Python Built-in Exceptions
Python provides many built-in exceptions that handle common errors. These help in identifying and fixing issues in code easily.
Python में बहुत सारे built-in exception होते हैं जो common errors को handle करते हैं। इससे error समझना और सुधारना आसान होता है।
1. ZeroDivisionError
try:
result = 10 / 0
except ZeroDivisionError:
print("You cannot divide by zero!")
2. ValueError
try:
num = int("abc")
except ValueError:
print("Invalid value for integer conversion!")
3. TypeError
try:
result = '10' + 5
except TypeError:
print("Cannot add string and integer!")
4. IndexError
try:
data = [1, 2, 3]
print(data[5])
except IndexError:
print("Index out of range!")
5. KeyError
try:
user = {"name": "Aryan"}
print(user["age"])
except KeyError:
print("Key not found in dictionary!")
6. FileNotFoundError
try:
f = open("missing.txt", "r")
except FileNotFoundError:
print("File does not exist!")