Skip to content

unexpected error #188

Answered by francium
dineshbvadhia asked this question in Q&A
Jun 23, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

There's an as_result, but otherwise there's no other builtin exception handling mechanism. I'd imagine most people fall back to try/catch when they're dealing with raw Python exceptions and converting them into a Result.

Here's a basic example,

import random
from result import as_result, Ok

def may_fail():
    if random.random() > 0.1:
        raise RuntimeError("Something unexpected happened")
    print("Done")

@as_result(BaseException)
def do_something():
    may_fail()

r = do_something()
if isinstance(r, Ok):
    pass
else:
    print(f"Something failed: {r.err_value}")

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@wbolster
Comment options

Answer selected by wbolster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants