0

I have a question regarding try/except. I have an exception (lets call it FooException) that has a status_code in it. I want to handle the exception just if the status_code is 200.

I would do something like:

try:
    ...
except FooException as ex:
    if ex.status_code == 200:
        # do something
    else:
        # do something else

Is there any other way or this one should go fine?

Thanks!

1

1 Answer 1

1

that's fine... Have your else: simply call raise, and it will simply re-raise the current exception, to be handled elsewhere. (or pass if you simply want to ignore.)

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah. Thanks, I was sure that works, but didn't know if there was another (more pythonic) way.
You could always create a subclass of FooException, say, Foo200Exception have your code throw that exception, but I'm all for readability and simplicity. Invent new stuff right before you need it & not before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.