Hone logo
Hone
Problems

Robust Error Handling with Exception Hooks

Exception hooks provide a powerful mechanism in Python to customize how uncaught exceptions are handled, particularly in production environments. This challenge asks you to implement exception hooks to log uncaught exceptions and potentially perform other actions like sending notifications, ensuring your application remains stable and provides valuable debugging information even when unexpected errors occur.

Problem Description

You are tasked with creating a system that utilizes Python's exception hooks (sys.excepthook and sys.sys.exc_clear) to gracefully handle uncaught exceptions. Your solution should:

  1. Define a Custom Exception Hook: Create a function named my_exception_hook that takes three arguments: sys, type, and value. This function will be used as the new exception hook.
  2. Log Exception Information: Inside my_exception_hook, log the exception type and the exception message to the console. The log message should be formatted as: "Uncaught Exception: [Exception Type] - [Exception Message]".
  3. Clear the Exception: After logging, call sys.exc_clear() to clear the exception information from the system. This prevents potential issues with subsequent code execution.
  4. Set the Exception Hook: Set sys.excepthook to your my_exception_hook function.
  5. Demonstrate Exception Handling: Within the main execution block, intentionally raise an exception (e.g., ZeroDivisionError) to trigger the exception hook. The program should not crash, but instead, the exception should be logged, and the program should continue (though further execution might be problematic depending on the context).

Examples

Example 1:

Input: No specific input, the program will intentionally raise an exception.
Output: Uncaught Exception: ZeroDivisionError - division by zero

Explanation: The program raises a ZeroDivisionError. The custom exception hook my_exception_hook is called, logs the error message to the console, and then clears the exception. The program continues execution after the hook.

Example 2:

Input: No specific input, the program will intentionally raise a ValueError.
Output: Uncaught Exception: ValueError - invalid literal for int() with base 10: 'abc'

Explanation: The program raises a ValueError. The custom exception hook my_exception_hook is called, logs the error message to the console, and then clears the exception. The program continues execution after the hook.

Example 3: (Edge Case - No Exception)

Input: No specific input, the program executes without raising an exception.
Output: (No output)

Explanation: If no exception is raised, the exception hook is never called, and no output is generated.

Constraints

  • The solution must be written in Python 3.
  • The my_exception_hook function must accept sys, type, and value as arguments.
  • The exception hook must log the exception type and message to the console.
  • The exception hook must call sys.exc_clear() after logging.
  • The program must not crash when an uncaught exception occurs.
  • The logging format must be exactly: "Uncaught Exception: [Exception Type] - [Exception Message]"

Notes

  • Remember to import the sys module.
  • Consider how this approach can be beneficial in production environments for centralized error logging and monitoring.
  • The sys.exc_clear() function is crucial for preventing unexpected behavior after the exception hook has been executed. Without it, the exception might still be present in the system, potentially affecting subsequent operations.
  • This challenge focuses on the basic implementation of exception hooks. More advanced scenarios might involve sending emails, writing to files, or integrating with external monitoring services.
Loading editor...
python