Skip to main content

优雅地处理python异常

Project description

TryTry

Package version Supported Python versions

中文文档

install

pip install trytry

Example

handle exception

from trytry import trytry


@trytry
def my_function():
    raise FileNotFoundError('file not found')


@trytry
def my_function2():
    print(1 / 0)


@trytry.exception(ZeroDivisionError)
def handle_zero_division_error(func, e):
    print(func.__name__, str(e))


@trytry.exception(FileNotFoundError)
def handle_file_not_found_error(func, e):
    print(func.__name__, str(e))


if __name__ == '__main__':
    my_function()
    my_function2()

handle all exception

from trytry import trytry


@trytry
def my_function():
    raise FileNotFoundError('file not found')


@trytry
def my_function2():
    print(1 / 0)


@trytry.exception(Exception)
def handle_all_error(func, e):
    print(func.__name__, str(e))


if __name__ == '__main__':
    my_function()
    my_function2()

All of the above exceptions are caught and are global exceptions. You can also catch exceptions for specific functions.

from trytry import trytry


@trytry
def my_function():
    print(1 / 0)

@my_function.exception(ZeroDivisionError)
def handle_zero_division_error(func, e):
    print(func.__name__, str(e))

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

trytry-0.1.2.tar.gz (2.2 kB view hashes)

Uploaded Source

Built Distribution

trytry-0.1.2-py3-none-any.whl (2.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page