This repository contains exercises, lab work, and an assignment completed as part of a Python bootcamp module on exception handling. The work focuses on writing robust programs that handle errors instead of crashing.
- Exception handling using
try,except,else, andfinally - Handling built-in exceptions such as:
ValueErrorZeroDivisionErrorFileNotFoundError
- Raising and catching custom exceptions
- Using loops for input validation
- Safe file handling
- User-friendly error reporting
- UML activity diagrams for exception flow
- Prompted the user for their age
- Used a
try...except ValueErrorblock to handle non-numeric input - Repeatedly asked for input until a valid age was entered
- Created a function that divides two numbers
- Used
try...except ZeroDivisionError - Returned
Nonewhen division by zero occurred
- Read numbers from a text file line by line
- Handled:
FileNotFoundErrorwhen the file does not existValueErrorwhen a line could not be converted to an integer
- Skipped invalid lines without crashing
- Drew a UML-style activity diagram to visualise:
- Normal execution flow (no exception)
- Exception raised and caught
- Guaranteed execution of the
finallyblock
- Extended a contact book application
- Raised a custom
KeyErrorwhen attempting to add a duplicate contact - Caught the exception in the main loop and displayed a user-friendly message
- Built a simple web scraper using the
requestslibrary - Prompted the user to enter a URL, automatically prepending
https://when missing - Implemented a
while True loopto allow multiple attempts without restarting the program - Added comprehensive exception handling to manage:
- Invalid or malformed URLs
- Network and connection errors
- Request timeouts
- HTTP status errors (e.g. 404, 500)
- SSL-related errors
- Ensured the program displays clear, user-friendly error messages rather than crashing the program
- Demonstrated correct ordering of exception handling to avoid overly broad catches
- Implemented
get_int_from_user(prompt)function - Used a
whileloop andtry...except ValueErrorfor validation - Re-prompted the user until a valid integer was entered
- Returned the valid integer
- Explained why bare
except:blocks should be avoided - Described execution flow of
try...except...finallywhen exceptions are uncaught - Provided a real-world scenario where
elseimproves readability
- Python
- Virtual Environments (
venv) - VS Code
- Pylint
- autopep8
- Git & GitHub
- Exception handling follows best practices by catching specific errors
- Resources are safely managed using
withstatements where applicable - The virtual environment is excluded using
.gitignore
This project demonstrates safe and structured error handling techniques commonly used in professional Python development.