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.

import logging
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.

import logging
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.

import logging
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 (not counting **kwargs).

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

The default behaviour is including the function arguments in your call, but you can choose not to send them. For example, you don't need this extra arguments in the logging.exception.

import logging
from la_catch import catch


@catch(Exception, logging.exception, include_args=False)
def example(a, b, c, d=1, e=2, f=3):
    raise Exception("You are a picky eater")

Metadata

Release files for la-catch 0.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 la-catch 0.0.3
File Size Uploaded Provenance Actions
la-catch-0.0.3.tar.gz 3.5 kB Without provenance Details for la-catch-0.0.3.tar.gz

Built distribution (wheel)

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

Total release size: 7.3 kB

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

Download URL la-catch-0.0.3.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ed4457f44f19820ee0b26b9ce6dd061bfd3eaf7e03f1af34ae2e2c204ac3dfe1
BLAKE2b-256 checksum
How to use checksums
9d07e5b92572aecc1ee34ab6e11141801f7a236690cf627b2fe96b195fc972a8
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.3-py3-none-any.whl

Download URL la_catch-0.0.3-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e44c4ed98c7c829ef440670fcfeca7c967b1cb92c135c73ffe472c0141cb1b9a
BLAKE2b-256 checksum
How to use checksums
ae61a1655d30ee17229266385700ebe66bd900dca65884f7c39494fe5d8f5e78
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.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.

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