Exception & Error Handling
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The exception and error handling mechanism within this project is designed to ensure robust data ingestion and processing for statistical analysis. The primary goal is to maintain the integrity of numerical datasets when reading from external text files, such as CHOLESTEROL.txt and WEIGHT.txt. By implementing specific error-catching logic, the system prevents application crashes caused by malformed or non-numeric data entries.
The system focuses on localizing errors during the file parsing phase. This ensures that only valid data points contribute to the final statistical calculations (mean, median, variance, etc.), while invalid entries are logged and discarded without halting the entire processing pipeline.
Sources: hw1/p1.py:1-12, hw1/CHOLESTEROL.txt
Data Ingestion and Validation
The core of the error handling strategy is located within the file opening and parsing logic. The system utilizes a protective try-except block to manage data conversion risks.
Numeric Conversion Safeguards
When the application iterates through lines in a source file, it attempts to cast each string entry into a floating-point number. This is the most critical failure point, as source files may contain trailing whitespace, empty lines, or non-numeric characters.
try:
data.append(float(line))
except ValueError:
print(f"Skipping non-numeric value: {line}")
Sources: hw1/p1.py:11-14
Handling Malformed Data
The following table describes the types of data inconsistencies handled by the system:
| Issue Type | Detection Method | Handling Strategy |
|---|---|---|
| Empty Lines | if line: check | Ignored; line is skipped before conversion attempt. |
| Whitespace | line.strip() | Leading/trailing spaces are removed before processing. |
| Non-numeric Text | ValueError exception | Logged to console with a "Skipping" message; item is excluded from dataset. |
Sources: hw1/p1.py:9-14, hw1/CHOLESTEROL.txt:29
Processing Logic Flow
The data flow involves a sequence from raw file reading to sanitized list generation. The error handling layer acts as a filter between the raw input and the statistical computation engine.
This diagram illustrates the iterative filtering process that protects the statistical functions from receiving None or string types which would cause runtime crashes in numpy or statistics modules.
Sources: hw1/p1.py:5-15
Error Propagation in Statistical Calculations
If the error handling in the ingestion phase fails to filter data correctly, the subsequent statistical modules will encounter errors. The system relies on the try-except block in open_file to ensure that the data list contains only valid floats before being passed to the following functions:
| Function | Expected Input | Potential Error if Unhandled |
|---|---|---|
np.mean(data) | Numeric Array | TypeError if non-numeric |
statistics.mode(data) | Numeric List | StatisticsError if empty |
np.std(data) | Numeric Array | TypeError |
Sources: hw1/p1.py:20-40
Conclusion
Exception and error handling in this project is characterized by a "fail-soft" approach during data entry. By capturing ValueError exceptions at the point of ingestion, the system ensures that the mathematical pipeline remains stable. This design is particularly effective for processing medical or scientific datasets where manual data entry errors (such as those observed in the whitespace at the end of CHOLESTEROL.txt) are common.
Sources: hw1/p1.py:13-14, hw1/CHOLESTEROL.txt:29