Skip to main content

Decorators that provide graceful exception handling, customization of error handling behavior, error logging

pip install ignoreexceptions

Tested against Windows 10 / Python 3.10 / Anaconda

The decorators break_KeyboardInterrupt, ignore_all_exceptions, and ignore_Exception offer several advantages in exception handling and error management. Here are some of the key advantages:

Graceful Exception Handling:

The decorators allow for graceful handling of exceptions by catching and controlling their impact on the program's execution. Instead of allowing exceptions to propagate and potentially crash the program, these decorators provide the ability to handle exceptions in a controlled manner.

Customized Exception Handling:

The decorators provide flexibility in defining custom exception handling behavior. Developers can specify alternative actions, return values, or error messages when exceptions occur. This allows for tailored exception handling strategies based on the specific needs of the application or use case.

Error Logging and Monitoring:

The decorators can log exceptions to a specified logfile, enabling error tracking, monitoring, and analysis. This facilitates the identification and resolution of errors by providing a centralized record of encountered exceptions.

Prevent Program Termination:

By handling exceptions, these decorators prevent the abrupt termination of the program. This is particularly useful in long-running processes, scripts, or automation tasks where a program crash would interrupt the entire workflow. Instead, the program can continue executing or gracefully exit based on the specified behavior.

Improved User Experience:

Graceful exception handling ensures that applications do not crash or display cryptic error messages to end-users. By providing alternative actions or error messages, the decorators can enhance the user experience by providing meaningful feedback or fallback mechanisms.

Debugging and Troubleshooting:

The decorators can be valuable during the debugging and troubleshooting phases of development. By catching and logging exceptions, developers can gain insights into the causes of errors, track down problematic code sections, and analyze the stack trace for detailed debugging information.

Robustness and Resilience:

These decorators contribute to the overall robustness and resilience of applications or systems. By handling exceptions and recovering from errors, the code becomes more fault-tolerant and can continue functioning in the presence of unexpected events or exceptional conditions.

Importing

from ignoreexceptions import ignore_all_exceptions, ignore_Exception, break_KeyboardInterrupt

Example 1: Using break_KeyboardInterrupt decorator

@break_KeyboardInterrupt
def test_while_loop():
    from time import sleep

    while True:
        print("oioi")
        sleep(1)


# In this example, the break_KeyboardInterrupt decorator is applied to the test_while_loop function.
# It ensures that if a KeyboardInterrupt exception is raised during the execution of the function,
# the loop will be interrupted and the program will not be terminated.

Example 2: Using ignore_all_exceptions decorator

@ignore_all_exceptions(
    v="dum**ss", print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def divide(a, b):
    return a / b


# In this example, the ignore_all_exceptions decorator is applied to the divide function.
# It handles all exceptions that may occur during the execution of the function.
# If an exception is caught, it prints the exception, logs it to a specified logfile,
# and returns the value "dum**ss" instead of raising the exception.

Example 3: Using ignore_Exception decorator

@ignore_Exception(
    v=list(), print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def process_data(l=(0, 1, 2, 3, 4, 5)):
    from random import choice

    results = []
    for _ in range(100000):
        results.append(10 / choice(l))
    return results


# The decorator @ignore_Exception(v="error occurred", print_exceptions=True, active=True, logfile='c:\\logtest.txt')
# is applied to the process_data function. It means that if any exception occurs during the execution of the function,
# the exception will be caught, printed (if print_exceptions is True), and logged to the specified logfile
# ('c:\logtest.txt'). Instead of raising the exception, the function will return the value "error occurred"
# (specified by v) as the result.
#
# Therefore, this function is designed to perform a potentially risky
# computation repeatedly and handle any exceptions that may occur during the process.

# Log file output:
# 2023/06/26 19:25:16.712902
# Traceback (most recent call last):
#   File "C:\ProgramData\anaconda3\envs\dfdir\di.py", line 125, in wrapper
#     result = func(*args, **kwargs)
#   File "C:\ProgramData\anaconda3\envs\dfdir\di.py", line 193, in process_data
#     results.append(10 / choice(l))
# ZeroDivisionError: division by zero
#
# ---------------------------

Combining decorators:

@break_KeyboardInterrupt
@ignore_Exception(
    v=list(), print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def process_data2(l=(0, 1, 2, 3, 4, 5)):
    from random import choice
    from time import sleep

    results = []
    for _ in range(100000):
        sleep(1)
        results.append(10 / choice(l[1:]))
    return results
    

Metadata

Release files for ignoreexceptions 0.11

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

Source distribution (sdist)

Source distribution for ignoreexceptions 0.11
File Size Uploaded Provenance Actions
ignoreexceptions-0.11.tar.gz 6.6 kB Without provenance Details for ignoreexceptions-0.11.tar.gz

Built distribution (wheel)

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

Total release size: 15.2 kB

Release files / ignoreexceptions-0.11.tar.gz

Download URL ignoreexceptions-0.11.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4190822979bfb26ea4b8d6330680d257c6f2b57ea67c37c0003f81196f63a5d2
BLAKE2b-256 checksum
How to use checksums
7c1a7a21876a4d813fa2d0c6be1b97ad214f3d759ea82534479173a109f7926c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / ignoreexceptions-0.11-py3-none-any.whl

Download URL ignoreexceptions-0.11-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a7fb609056920319069448f443c7bbe57a2ce08d2f2daa83340a0cbd4b1de3e
BLAKE2b-256 checksum
How to use checksums
5331412f28ad16772ed909e9580b78db82acc5d9dd91fa1af13d44d43e6e126a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for ignoreexceptions 0.11

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.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

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