Skip to content

incendium.exceptions.ApplicationError

César Román edited this page Feb 11, 2022 · 11 revisions

Description

ApplicationError inherits from Exception. This type of exception is to be raised whenever an exception must be wrapped.

Syntax

ApplicationError(message, inner_exception, [cause])

Args:

  • message (str): The error message.
  • inner_exception (object): The inner Exception.
  • cause (str): The cause of the Exception. Optional. Defaults to None.

Recommendations

PEP8 recommends the following:

When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause.

For example, use:

try:
    import platform_specific_module
except ImportError:
    platform_specific_module = None

A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:).

A good rule of thumb is to limit use of bare 'except' clauses to two cases:

If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred. If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise. try...finally can be a better way to handle this case.

Code Examples

Call from the UI; from a button's actionPerformed code.

from incendium import gui
from incendium.exceptions import ApplicationError

try:
    # Call some function.
    app.some_important_function()
except ApplicationError as e:
    gui.error(e.message, "Error")

And the some_important_function would look like this:

import traceback

from incendium import constants, util
from incendium.exceptions import ApplicationError
from java.lang import Exception as JException


def some_important_function():
    """Very important function."""
    try:
        # TODO: Do something very important.
        pass
    except JException as e:
        message = constants.UNEXPECTED_ERROR_CAUSED_BY.format(
            util.get_function_name(),
            "\n".join(traceback.format_exc().splitlines()),
            e.cause,
        )
        raise ApplicationError(message, e, e.cause)
    except (IndexError, KeyError) as e:
        message = constants.UNEXPECTED_ERROR.format(
            util.get_function_name(),
            "\n".join(traceback.format_exc().splitlines()),
        )
        raise ApplicationError(message, e)
Clone this wiki locally