Skip to main content

la-catch

Decorator to catch exception(s) and call function to handle the error. Can also define a return value to the function after catching.

install

pip install la-catch

syntax

@catch(exceptions, call, ret)

exceptions - Exception or Tuple of exceptions to be captured
call - function to be called in case of exceptions being raised
ret - value to be returned by the main function in case of exceptions being raised

call receives the same arguments from the decorated function but it appends the exception raised to the list of arguments (*args).
In case an exception is raised and no ret was defined, the return will be the same that call would return.

usages

Let's say that you just expect a function to try running something and if fails ignore and continue the program logic. In this case you just want to catch and ignore the exception:

from la_catch import catch


@catch(Exception)
def example():
    raise Exception("What a great day to raise an exception")

Most of times you want at least to print the exception, in this case you can pass a function to be called. This function will receive the exception as the last argument for you use as you like.

from la_catch import catch


def func(e):
    print("Look what i catched mommy:", e)


@catch(Exception, func)
def example():
    raise Exception("You will never catch me alive")

If all that you want is print the exception/traceback, i would recommend you passing logging.exception as call.

from la_catch import catch


@catch(Exception, logging.exception)
def example():
    raise Exception("I love to read tracebacks")

Let's say that you know how to deal with the raised problem, now you want the function to return the solution of the problem. You can make call return the expected resolution.

from la_catch import catch


def func(e):
    if isinstance(e, ZeroDivisionError):
        print("You can't divide by zero you dummy")
        return 0
    return 1


@catch(Exception, func)
def example():
    return 0/0

Okay okay, but now you want to log the exception and return something so you program doesn't crash. Better than creating a function just for this two things is passing to ret the value to return in case of this exception.

from la_catch import catch


@catch(Exception, logging.exception, 0)
def example():
    return 0/0

Of course that i am using Exception everywhere but you could use other exception or multiple exceptions!

from la_catch import catch


@catch((ZeroDivisionError, OverflowError, FloatingPointError), logging.exception, 0)
def example():
    return 0/0

If you want to make different things to different exceptions just decorate with one more catch.

from la_catch import catch


@catch(OverflowError, logging.exception, None)
@catch(ZeroDivisionError, logging.exception, 0)
def example():
    return 0/0

Just remember that exception is always pass as the last argument from call.

from la_catch import catch


def func(a, b, e):
    print("I have zero tolerance for this error... got it?")


@catch(ZeroDivisionError, func, 0)
def example(a, b):
    return a/b

Metadata

Release files for la-catch 0.0.1

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

Source distribution (sdist)

Source distribution for la-catch 0.0.1
File Size Uploaded Provenance Actions
la-catch-0.0.1.tar.gz 3.3 kB Without provenance Details for la-catch-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for la-catch 0.0.1
File Interpreter ABI Platform Actions
la_catch-0.0.1-py3-none-any.whl Python 3 none any Details for la_catch-0.0.1-py3-none-any.whl

Total release size: 6.9 kB

Release files / la-catch-0.0.1.tar.gz

Download URL la-catch-0.0.1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
14cc702dd2f2c139d16a46d6c65bf78af244d81ee7e677cb6daad63a764cab85
BLAKE2b-256 checksum
How to use checksums
969b9bcc73fd7ebb06a532b6b991ef854a04326e475d23ab320b3f2bc02f1d34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / la_catch-0.0.1-py3-none-any.whl

Download URL la_catch-0.0.1-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
841917eef97ab4381ae4f3f9012e865db43d99e43bb38005c2c398e47428c6ac
BLAKE2b-256 checksum
How to use checksums
8542acc3aa50296063590b6a2447a285c0b645a0b355bef1038e068515aaf892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for la-catch 0.0.1

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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