# Week 8 - Lab: Python Program # Assignment Value: 10 points # Description: # Create a Python program that demonstrates exception handling. # Your program should include at least three custom-defined exceptions and handle them appropriately. # Implement try-except blocks to handle exceptions and provide meaningful error messages. # Test your program with various scenarios to ensure proper exception handling. # Instructions: # Create a Python script named exception_handling.py. # Define three custom exceptions: CustomError1, CustomError2, and CustomError3. # Implement a function divide_numbers() that takes two numbers as input and returns the result of dividing the first number by the second number. # Handle the ZeroDivisionError exception. If the second number is zero, raise CustomError1. # Implement a function open_file() that takes a filename as input and tries to open the file. # Handle the FileNotFoundError exception. If the file does not exist, raise CustomError2. # Implement a function process_data() that takes a list as input and tries to access an element at a specific index. # Handle the IndexError exception. If the index is out of range, raise CustomError3. # In the main part of your script, demonstrate the usage of all the implemented functions by invoking them and handling exceptions appropriately. # Take a screenshot of Thonny showing both the code and output at the end of the exercise. # Upload the screenshot. # Directions for submitting your written assignment as an attachment: # When you have completed the assignment, save the file on your computer with your name as part of the file name, for example, a_student_assignment1.docx [using your first initial and last name] # Click on the title of the assignment above. # Click on "Upload Files". # Click on the "Browse Local Files" button to attach your file to this assignment. # Once you have attached your file, click on the "Submit" button to send your file to your instructor. # Note: This assignment is due 11:59 PM ET, Sunday of Week 8. import os def divide_numbers(num1, num2): try: print(f"Dividing numbers {num1} / {num2}") if num2 == 0: raise CustomError1 return num1 / num2 except CustomError1: print("CustomError1: Cannot divide by zero.") def open_file(input_file): try: print(f"Trying to open the {input_file}") with open(input_file, 'r') as f: print(f"{input_file} has been opened successfully.") pass except FileNotFoundError: raise CustomError2 def process_data(data, index): try: print(f"Trying to return the data for each index.") return data[index] except IndexError: raise CustomError3 class CustomError1(Exception): pass class CustomError2(Exception): pass class CustomError3(Exception): pass def __main__(): script_directory = os.path.dirname(os.path.abspath(__file__)) # get the directory of the script input_file = os.path.join(script_directory, "input.txt") #had to use os.path.join to get the correct path. Wasn't working with just the file name fail_input_file = os.path.join(script_directory, "doesnotexist.txt") try: print("\n\n----------Division Function-------------") print("----------------------------------------") print(f"Failure case: {divide_numbers(10, 0)}") print(f"Success case: {divide_numbers(100, 10)}") except CustomError1: print("CustomError1 handled.") print("----------End Division Function---------\n\n") try: print("---------Open File Function-------------") print("----------------------------------------") print(f"Success Case: Trying to open {input_file}") open_file(input_file) print(f"Fail Case: Trying to open {fail_input_file}") open_file(fail_input_file) except CustomError2: print("CustomError2 handled.") print("---------End Open File Function---------\n\n") try: print("-------Process List Input Function------") print("----------------------------------------") print(f"Failure case: {process_data([1, 2, 3], 3)}") print(process_data([1, 2, 3], 2)) except CustomError3: print("CustomError3 handled.") try: print("-------Process List Input Function------") print("----------------------------------------") print(f"Success case: {process_data([1, 2, 3], 2)}") except CustomError3: print("CustomError3 handled.") print("----End Process List Input Function-----\n\n") if __name__ == "__main__": __main__()