Crafting Custom Exceptions in Python
Custom exceptions allow you to define specific error types tailored to your application's logic, making error handling more precise and informative. This challenge will guide you through creating and raising custom exceptions in Python, enhancing the robustness and clarity of your code. Understanding custom exceptions is crucial for building well-structured and maintainable applications.
Problem Description
You are tasked with designing a system for managing inventory in a warehouse. This system needs to handle various error conditions, such as attempting to retrieve an item that doesn't exist, trying to add an item with a negative quantity, or encountering invalid data types during processing. To improve error handling and provide more context, you need to create custom exception classes for these scenarios.
Specifically, you must:
- Create three custom exception classes:
ItemNotFoundException,InvalidQuantityException, andInvalidDataTypeException. ItemNotFoundExceptionshould inherit fromException. It should accept anitem_idas an argument during initialization and store it.InvalidQuantityExceptionshould also inherit fromException. It should accept aquantityas an argument during initialization and store it.InvalidDataTypeExceptionshould inherit fromException. It should accept adata_typeas an argument during initialization and store it.- Write a function
process_inventory(item_id, quantity, data)that simulates inventory processing. This function should:- Check if the
item_idexists (assume a simple check:item_idmust be a positive integer). If not, raiseItemNotFoundExceptionwith the givenitem_id. - Check if the
quantityis a positive integer. If not, raiseInvalidQuantityExceptionwith the givenquantity. - Check if the
datais a string. If not, raiseInvalidDataTypeExceptionwith the string "string". - If all checks pass, print a success message indicating the item has been processed.
- Check if the
Examples
Example 1:
Input: process_inventory(123, 5, "valid_data")
Output: Item processed successfully.
Explanation: All inputs are valid, so the function prints the success message.
Example 2:
Input: process_inventory("abc", 10, "data")
Output: ItemNotFoundException: Item ID 'abc' is invalid.
Explanation: The item_id is not a positive integer, so ItemNotFoundException is raised.
Example 3:
Input: process_inventory(456, -2, "data")
Output: InvalidQuantityException: Quantity -2 is invalid.
Explanation: The quantity is negative, so InvalidQuantityException is raised.
Example 4:
Input: process_inventory(789, 3, 123)
Output: InvalidDataTypeException: Data type is not a string.
Explanation: The data is not a string, so InvalidDataTypeException is raised.
Constraints
item_idmust be a string or integer.quantitymust be an integer.datamust be of any type.- The
item_idmust be a positive integer to be considered valid. - The
quantitymust be a positive integer to be considered valid.
Notes
- Remember to inherit your custom exceptions from the
Exceptionclass. - Consider using the
__init__method to initialize your custom exceptions with relevant information. - The
process_inventoryfunction should only raise exceptions; it should not handle them. The calling code is responsible for handling the exceptions. - Focus on creating clear and informative exception messages.
- The
process_inventoryfunction does not need to actually interact with any inventory data; it only needs to perform the validation checks and raise the appropriate exceptions.