--- id: practical-python-3.9 source_exercise_id: "3.9" title: "Catching exceptions" section: "3.3 Error Checking" source_path: "03_Program_organization/03_Error_checking.md" source_repo: "https://github.com/dabeaz-course/practical-python" source_commit: "93dca856b41c61a0a0f85ae334116e4c125629ea" student_visible_solution: false has_private_solution: false skip: false --- # Exercise 3.9: Catching exceptions > Source: Practical Python Programming, `03_Program_organization/03_Error_checking.md`. ### Exercise 3.9: Catching exceptions The `parse_csv()` function you wrote is used to process the entire contents of a file. However, in the real-world, it’s possible that input files might have corrupted, missing, or dirty data. Try this experiment: ```python >>> portfolio = parse_csv('Data/missing.csv', types=[str, int, float]) Traceback (most recent call last): File "", line 1, in File "fileparse.py", line 36, in parse_csv row = [func(val) for func, val in zip(types, row)] ValueError: invalid literal for int() with base 10: '' >>> ``` Modify the `parse_csv()` function to catch all `ValueError` exceptions generated during record creation and print a warning message for rows that can’t be converted. The message should include the row number and information about the reason why it failed. To test your function, try reading the file `Data/missing.csv` above. For example: ```python >>> portfolio = parse_csv('Data/missing.csv', types=[str, int, float]) Row 4: Couldn't convert ['MSFT', '', '51.23'] Row 4: Reason invalid literal for int() with base 10: '' Row 7: Couldn't convert ['IBM', '', '70.44'] Row 7: Reason invalid literal for int() with base 10: '' >>> >>> portfolio [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}] >>> ``` ## 关联来源 - [[summaries/03_Error_checking]]