A lightweight decorator for automatic exception handling and logging in Python.
Project description
🧰 ThrowPy
ThrowPy is a lightweight Python library that provides a simple and elegant way to make any function or class method "throwable", similar to Java-style exception handling.
It offers an easy-to-use decorator — @ThrowableExcept — that automatically catches exceptions, prints meaningful error messages, and optionally logs them to a file.
Features
- 🔹 Simple decorator-based usage
- 🔹 Automatic exception catching with file and line context
- 🔹 Optional file logging (
logErr=True) - 🔹 Works with both functions and class methods
- 🔹 Minimal dependencies (pure Python)
- 🔹 Python 3.8+ compatible
1. Installation
You can install ThrowPy directly from PyPI:
pip install throwpy
2. Import and Basic Usage
The decorator can be applied to functions or methods.
from throwpy import ThrowableExcept
When a decorated function raises an exception, the decorator intercepts it, generates a structured error message, and: prints it to the console (stdout) if logErr=True, writes it to a specified log file The decorator returns None when an exception occurs (i.e., it does not re-raise the error).
3. Decorator Parameters
logErr (bool)
Purpose: Enables or disables writing the error message to a file. Default: True (if not specified, logging to file is enabled).
If logErr=False, the decorator will not attempt to write to a file, but the error message will still be printed to the console.
logfile (string)
Purpose: Specifies the path/name of the log file where error messages are appended. Default: "logErr.log" (typically created in the current working directory).
If logErr=True but the logfile cannot be written to (e.g., due to missing permissions), an error will be caught and a message such as [LOGGING ERROR] Unable to write log: … will be printed to the console.
4. Usage Examples
4.1 Simple Function with Default File Logging
from throwpy import ThrowableExcept
@ThrowableExcept() # equivalent to logErr=True, logfile="logErr.log"
def divide(a, b):
return a / b
divide(10, 0)
Behavior: The ZeroDivisionError is intercepted. A message similar to the following is generated:
[2025-11-04 18:00:00.123456][ERROR] script.py:12 <no class>.divide() -> division by zero
The message is printed to the console and appended to the file logErr.log (created if it doesn’t exist).
4.2 Disabling File Logging
@ThrowableExcept(logErr=False)
def get_item(lst, idx):
return lst[idx]
get_item([], 5)
Behavior: The IndexError is intercepted. The message is printed to the console, but not written to any file.
4.3 Using a Custom Log File
@ThrowableExcept(logErr=True, logfile="errors/my_errors.log")
def risky_operation(x):
return 10 / x
risky_operation(0)
Behavior: If the errors/ directory exists and my_errors.log is writable, the log will be written there. If writing fails (e.g., directory doesn’t exist or permissions are missing), a message like:
[LOGGING ERROR] Unable to write log: <internal error>
will be printed.
4.4 Usage in Classes (Methods)
class Example:
@ThrowableExcept()
def risky(self, x):
return 100 / x
ex = Example()
ex.risky(0)
Behavior: The generated message will include the class name:
[2025-11-04 18:05:34.567890][ERROR] myfile.py:45 Example.risky() -> division by zero
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file throwpy-1.0.3.tar.gz.
File metadata
- Download URL: throwpy-1.0.3.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22aefd042c18591281e42f821c2c3f576c5dbe83068fce5d3439fe2064ff5ec1
|
|
| MD5 |
e3e3d8f3d9956f7726a96b6c4c85a797
|
|
| BLAKE2b-256 |
b9fcdb513d05c03a88e18bb1a81dc5699f4f4d1f07ecdcc23ae1e9fd1850445b
|
File details
Details for the file throwpy-1.0.3-py3-none-any.whl.
File metadata
- Download URL: throwpy-1.0.3-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f75970653837502e1e48276194f034aca09cbff09f2e3111b9db1ae58582133a
|
|
| MD5 |
cca960887e795ceba8de95aece5ef906
|
|
| BLAKE2b-256 |
9acac22bf8fee6b10bb0dc0cc421e2962907396d9302601ee140f59ddd8bae5e
|