Skip to main content

🧰 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

Metadata

Release files for throwpy 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for throwpy 1.0.3
File Size Uploaded Provenance
throwpy-1.0.3.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for throwpy 1.0.3
File Interpreter ABI Platform
throwpy-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / throwpy-1.0.3.tar.gz

Download URL throwpy-1.0.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
22aefd042c18591281e42f821c2c3f576c5dbe83068fce5d3439fe2064ff5ec1
BLAKE2b-256 checksum
How to use checksums
b9fcdb513d05c03a88e18bb1a81dc5699f4f4d1f07ecdcc23ae1e9fd1850445b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / throwpy-1.0.3-py3-none-any.whl

Download URL throwpy-1.0.3-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f75970653837502e1e48276194f034aca09cbff09f2e3111b9db1ae58582133a
BLAKE2b-256 checksum
How to use checksums
9acac22bf8fee6b10bb0dc0cc421e2962907396d9302601ee140f59ddd8bae5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for throwpy 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page