unexpected error #188
Answered
by
francium
dineshbvadhia
asked this question in
Q&A
-
Have looked at the doc but cannot see (or understand) how to capture an unexpected error and its full traceback? Generally, on any Err() how do you capture the full traceback? |
Beta Was this translation helpful? Give feedback.
Answered by
francium
Jun 23, 2024
Replies: 1 comment 1 reply
-
There's an 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}") |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
wbolster
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,