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!

porthunt
  • 484
  • 1
  • 5
  • 12
  • Possible duplicate of [Python: Catching specific exception](http://stackoverflow.com/questions/13531247/python-catching-specific-exception) – TemporalWolf Mar 10 '17 at 23:00

1 Answers1

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

pbuck
  • 4,291
  • 2
  • 24
  • 36
  • Yeah. Thanks, I was sure that works, but didn't know if there was another (more pythonic) way. – porthunt Mar 10 '17 at 23:04
  • 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. – pbuck Mar 10 '17 at 23:12