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

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

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 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.

Source Distribution

raise-1.1.7.tar.gz (4.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

raise-1.1.7-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

raise-1.1.7-py2-none-any.whl (3.3 kB view details)

Uploaded Python 2

File details

Details for the file raise-1.1.7.tar.gz.

File metadata

  • Download URL: raise-1.1.7.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for raise-1.1.7.tar.gz
Algorithm Hash digest
SHA256 14c9501c60b2494200cd8c5048fd34c4383f53dc56ce21794a6ee34b3d100a5b
MD5 79b30fb4edcd51c1daa753bfd9a604dd
BLAKE2b-256 9ae224a74211329257495b966dd6950bd1e5ac695bc429eab84bf670d137d1ba

See more details on using hashes here.

File details

Details for the file raise-1.1.7-py3-none-any.whl.

File metadata

  • Download URL: raise-1.1.7-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for raise-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7b00e3108b581cab4e401ea5b42feb764bb963ae32b6a4ab72ce11f88c3ad8ba
MD5 d25f89806ac17a72f490ce2d880de36e
BLAKE2b-256 4b5aa8760f41a7c1e080dfe570a875a13b1300623437d86a877d852e902ec9e3

See more details on using hashes here.

File details

Details for the file raise-1.1.7-py2-none-any.whl.

File metadata

  • Download URL: raise-1.1.7-py2-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for raise-1.1.7-py2-none-any.whl
Algorithm Hash digest
SHA256 83dbd5379293963b9a879ed20c49ec57c8df01761d07ce38b9c61f3aa44cbb6c
MD5 921c06bea4747cff4436daf2e39ee90b
BLAKE2b-256 a75bdb2710671f0117bf4f1d1bf0bb1393d6a0919415b608723fbbcb44ba60ba

See more details on using hashes here.

Supported by

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