Skip to main content

Raise exceptions with a function instead of a statement.

Project description

Raise exceptions with a function instead of a statement.

Provides a minimal and portable interface for raising exceptions with all the advantages of functions over syntax.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install raise

Usage

Import raise_:

from raise_ import raise_

Raise an exception:

raise_(Exception('foo'))

Raise an exception with a traceback:

raise_(Exception('foo'), traceback)

Raise in a lambda:

lambda x: x if x > 0 else raise_(ValueError('x is too small!'))

And of course because raise_ is a function, you can combine it with functools.partial and other functional programming libraries and techniques for many more uses.

Surprises

raise_ clears __traceback__ if you don’t pass in a traceback, same as if you passed in None. If you want the Python 3 behavior of reusing the __traceback__, you should explicitly pass it in:

raise_(exception, exception.__traceback__)

Or, if you want to gracefully degrade on Python implementations which do not have __traceback__ on their exceptions:

raise_(exception, getattr(exception, '__traceback__', None))

Portability

Portable to all releases of both Python 3 and Python 2.

(The oldest tested is 2.5, but it will likely work on all Python 2 versions and probably on even earlier versions.)

For implementations of Python that do not support raising with a custom traceback, a “no traceback” variant is included in the source that can be installed manually.

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

raise-1.1.9.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distributions

raise-1.1.9-py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 3

raise-1.1.9-py2-none-any.whl (3.0 kB view hashes)

Uploaded Python 2

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