3

eg:

try:
    myfruits = FruitFunction()    #Raises some exception (unknown)
    assert "orange" in myfruits   #Raises AssertionError (known)

except:
    # I don't know how to distinguish these two errors :(

I need to filter out one particular kind of exception (which is known) from all others that are unknown. I also need assertion to continue the same exception handling,

try:
    myfruits = FruitFunction()    #Raises some exception (unknown)
    assert "orange" in myfruits   #Raises AssertionError (known)

except AssertionError:
    # Handle Assertion Errors here
    # But I want the except below to happen too!

except:
    # Handle everything here

I will add one real example to convey the idea more concisely:

try:
    # This causes all sorts of errors
    myurl = urllib.urlopen(parametes)

    # But if everything went well
    assert myurl.status == 202

    # proceed normal stuff

except:
    # print "An error occured" if any error occured
    # but if it was an assertion error,
    # add "it was something serious too!"
  • 3
    answer here http://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block – Daniel Dec 03 '13 at 22:23
  • 1
    I've edited my quetion, please look in to it, it's slightly different. –  Dec 03 '13 at 22:32

3 Answers3

5
try:
    try:
        myfruits = FruitFunction()    #Raises some exception (unknown)
        assert "orange" in myfruits   #Raises AssertionError (known)
    except AssertionError:
        # handle assertion
        raise
except Exception:
    # handle everything

I'm assuming you can't separate the two statements that throw the different exceptions (because they're off together in another function, for example). If you can then the following is more precise and straightforward:

try:
    myfruits = FruitFunction()    #Raises some exception (unknown)
    try:
        assert "orange" in myfruits   #Raises AssertionError (known)
    except AssertionError:
        # handle assertion
        raise
except Exception:
    # handle everything

It's more precise because if the unknown exception raised by FruitFunction() happens to be AssertionError then it won't get caught in the inner try. Without separating the statements, there's no (sensible) way to distinguish two exceptions of the same type thrown from the two different places. So with the first code you'd better hope that FruitFunction() either doesn't raise AssertionError, or that if it does then it can be handled the same way as the other one.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • 2
    I tried to avoid nesting try-except statements for readability. If I did follow that pattern, I would end up with a 5 to 6 step nested code! Besides, asserting instead of if-else statement also contribute to readability and linear coding. But, thank you very much for your answer and time. –  Dec 10 '13 at 12:07
3
try:
    # Do something
    myurl = urllib.urlopen(parametes)
    assert myurl.status == 202
except Exception as e:
    # Handle Exception
    print('An error occured')
    if isinstance(e, AssertionError):
        # Handle AssertionError
        print('it was something serious too!')
else:
    # proceed normal stuff
Remco Haszing
  • 7,178
  • 4
  • 40
  • 83
0

In the case that it's both raised an unknown exception and an AssertionError, and you need to handle both, you should use two separate try statements.

try:
    raise IndexError()
    assert 'orange' in myfruits
except AssertionError:
    print 'AssertionError'
except:
    print 'another error'

The above will only catch the first error, while

try:
    raise IndexError()
except:
    print 'another error'
try:
    assert 'orange' in myfruits
except AssertionError:
    print 'AssertionError'

Will catch both errors.

Steinar Lima
  • 7,644
  • 2
  • 39
  • 40