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, clean, 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

If you need to get it manually, or you need the “no traceback” variant, see the Manual Installation section for tips.

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

In Python 3, the raise statement reuses the __traceback__ attribute every time it raises an exception unless you explicitly set another traceback. The raise_ function does not reuse __traceback__, and instead clears it if you do not pass in a traceback, 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 can be installed manually.

Manual Installation

Depending on your needs, either:

  • Take one of these files and save it as raise_.py:
    • raise_3.py is for Python 3.
    • raise_2.py is for Python 2.
    • raise_no_traceback.py is for Python implementations which do not support raising exceptions with a custom traceback.
  • Take the above files that you need, and save them in a folder called raise_ along with a custom __init__.py that conditionally imports from the right file as needed.

That way you can always do from raise_ import raise_ in all of your other code and it’ll just work.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for raise, version 1.1.3
Filename, size File type Python version Upload date Hashes
Filename, size raise-1.1.3-py2-none-any.whl (3.7 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size raise-1.1.3-py3-none-any.whl (3.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size raise-1.1.3.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page