EOF ERROR:
try:
file = open("data.txt", "r")
line = file.readline()
print("Line from file:", line.strip())
user_input = input("Enter something: ")
print("You entered:", user_input)
except EOFError:
print("EOFError: No input received (input stream ended).")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
file.close()
Input/Output Error:
try:
file = open("non_existing_folder/data.txt", "w")
file.write("Testing IOError handling.")
print("Write successful.")
except IOError:
print("IOError: An input/output error occurred (e.g., file not found or
inaccessible).")
finally:
try:
file.close()
except:
pass
Name Error:
try:
file = open("data.txt", "r")
content = file.read()
print("File content:", content)
print("The value is:", value) # <-- 'value' is not defined, causes NameError
except NameError:
print("NameError: You are trying to use a variable that has not been
defined.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
Index Error:
try:
file = open("data.txt", "r")
lines = file.readlines()
print("Line at index 5:", lines[5]) # This may raise IndexError if file has < 6
lines
except IndexError:
print("IndexError: You tried to access a list index that doesn't exist.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
Name Error:
try:
# Open the file
file = open("data.txt", "r")
content = file.read()
print("File content:", content)
# Intentional NameError: using an undefined variable
print("Undefined variable value is:", result) # 'result' is not defined
except NameError:
print("NameError: You tried to use a variable that was never defined.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
Type Error:
try:
# Open the file and read content
file = open("data.txt", "r")
content = file.read()
print("File content:", content)
# Intentional TypeError: trying to add a string and an integer
result = content + 5 # This will raise TypeError
print("Result:", result)
except TypeError:
print("TypeError: You tried to perform an invalid operation between different
data types.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
Value Error:
try:
# Open the file and read a number
file = open("data.txt", "r")
content = file.read().strip()
print("File content:", content)
# Try converting the content to an integer
number = int(content) # Will raise ValueError if content is not a valid integer
print("Converted number:", number)
except ValueError:
print("ValueError: Could not convert the file content to an integer.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
ZeroDivivsion Error:
try:
# Read a number from a file
file = open("data.txt", "r")
content = file.read().strip()
number = int(content)
print("Number from file:", number)
# Intentional ZeroDivisionError if number is 0
result = 100 / number
print("Result:", result)
except ZeroDivisionError:
print("ZeroDivisionError: You tried to divide by zero.")
except ValueError:
print("ValueError: The file does not contain a valid integer.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
Overflow Error:
import math
try:
# Read a number from a file
file = open("data.txt", "r")
content = file.read().strip()
number = int(content)
print("Number from file:", number)
# Intentional OverflowError: raise a large number to a high power
result = math.exp(number) # math.exp(1000) or higher causes
OverflowError
print("Result:", result)
except OverflowError:
print("OverflowError: The number is too large to handle mathematically.")
except ValueError:
print("ValueError: The file does not contain a valid integer.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists
KeyError:
try:
# Open and read a key from a file
file = open("data.txt", "r")
key = file.read().strip()
print("Key from file:", key)
# A sample dictionary
student_marks = {
"Alice": 85,
"Bob": 92,
"Charlie": 78
}
# Try to access a key that might not exist
print("Mark of student:", student_marks[key]) # Raises KeyError if key not
found
except KeyError:
print("KeyError: The specified key does not exist in the dictionary.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass
Keyboard Interrupt Error:
try:
# Simulate reading from a file (not necessary for this error, but included)
file = open("data.txt", "r")
content = file.read().strip()
print("File content:", content)
# Prompt user input — press Ctrl+C here to trigger KeyboardInterrupt
name = input("Enter your name (press Ctrl+C to interrupt): ")
print("Hello,", name)
except KeyboardInterrupt:
print("\nKeyboardInterrupt: The program was interrupted by the user.")
except FileNotFoundError:
print("File not found. Please make sure 'data.txt' exists.")
finally:
try:
file.close()
except:
pass