Hone logo
Hone
Problems

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:

  1. Create three custom exception classes: ItemNotFoundException, InvalidQuantityException, and InvalidDataTypeException.
  2. ItemNotFoundException should inherit from Exception. It should accept an item_id as an argument during initialization and store it.
  3. InvalidQuantityException should also inherit from Exception. It should accept a quantity as an argument during initialization and store it.
  4. InvalidDataTypeException should inherit from Exception. It should accept a data_type as an argument during initialization and store it.
  5. Write a function process_inventory(item_id, quantity, data) that simulates inventory processing. This function should:
    • Check if the item_id exists (assume a simple check: item_id must be a positive integer). If not, raise ItemNotFoundException with the given item_id.
    • Check if the quantity is a positive integer. If not, raise InvalidQuantityException with the given quantity.
    • Check if the data is a string. If not, raise InvalidDataTypeException with the string "string".
    • If all checks pass, print a success message indicating the item has been processed.

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_id must be a string or integer.
  • quantity must be an integer.
  • data must be of any type.
  • The item_id must be a positive integer to be considered valid.
  • The quantity must be a positive integer to be considered valid.

Notes

  • Remember to inherit your custom exceptions from the Exception class.
  • Consider using the __init__ method to initialize your custom exceptions with relevant information.
  • The process_inventory function 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_inventory function does not need to actually interact with any inventory data; it only needs to perform the validation checks and raise the appropriate exceptions.
Loading editor...
python