Exceptions

x = 10 / 0  # Raises ZeroDivisionError
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[1], line 1
----> 1 x = 10 / 0  # Raises ZeroDivisionError

ZeroDivisionError: division by zero
x = "Hello" + 5  # Raises TypeError: can only concatenate str (not "int") to str
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 x = "Hello" + 5  # Raises TypeError: can only concatenate str (not "int") to str

TypeError: can only concatenate str (not "int") to str
x = int("abc")  # Raises ValueError: invalid literal for int() with base 10: 'abc'
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 x = int("abc")  # Raises ValueError: invalid literal for int() with base 10: 'abc'

ValueError: invalid literal for int() with base 10: 'abc'
numbers = [1, 2, 3]
print(numbers[5])  # Raises IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[4], line 2
      1 numbers = [1, 2, 3]
----> 2 print(numbers[5])  # Raises IndexError: list index out of range

IndexError: list index out of range
my_dict = {"name": "John", "age": 25}
print(my_dict["city"])  # Raises KeyError: 'city'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[5], line 2
      1 my_dict = {"name": "John", "age": 25}
----> 2 print(my_dict["city"])  # Raises KeyError: 'city'

KeyError: 'city'
file = open("nonexistent.txt")  # Raises FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent.txt'
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[6], line 1
----> 1 file = open("nonexistent.txt")  # Raises FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent.txt'

File /usr/local/Caskroom/miniconda/base/envs/llm/lib/python3.9/site-packages/IPython/core/interactiveshell.py:284, in _modified_open(file, *args, **kwargs)
    277 if file in {0, 1, 2}:
    278     raise ValueError(
    279         f"IPython won't let you open fd={file} by default "
    280         "as it is likely to crash IPython. If you know what you are doing, "
    281         "you can use builtins' open."
    282     )
--> 284 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent.txt'
try:
    x = 10 / 0  # Division by zero raises an exception
except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")
Error: Division by zero is not allowed.
try:
    x = int(input("Enter a number: "))
    result = 10 / x
except ValueError:
    print("Error: Invalid input. Please enter a valid number.")
except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")
else:
    print("The result is:", result)
finally:
    print("Thank you for using the program!")