3

I have a code like this:

try:
    ....

    l.simple_bind_s(user, password)

except ldap.CONNECT_ERROR, e:
    sys.exit(1)

except ldap.BUSY, e:
    sys.exit(2)

except ldap.OPT_NETWORK_TIMEOUT, e:
    sys.exit(3)

except ldap.TIMEOUT, e: 
    sys.exit(4)

except ldap.SERVER_DOWN, e:
    sys.exit(5)

I am trying to catch various kinds of exceptions. However all exceptions fall in SERVER_DOWN. When, for example, there is a timeout exception it falls into SERVER_DOWN exception, etc. I wonder if there is something like a hierarchy of exceptions and that's why it always falls into SERVER_DOWN state. Or is there any other problem with this code? Do you have any opinion about this issue? Thanks in advance.

Matt McClure
  • 16,009
  • 2
  • 28
  • 30
kursat
  • 1,059
  • 5
  • 15
  • 32
  • 1
    Have you checked the actual type of the caught exception to make sure it is ldap.TIMEOUT and not ldap.SERVER_DOWN with a message stating it is a timeout? – Manfre Jul 12 '11 at 14:21
  • i solve the problem. i checked it, it is server down. just i tested it in a wrong way. i gave bind method an ip not existed. timeout is for search method. thanks – kursat Jul 16 '11 at 11:51

2 Answers2

2

Yes there is a hierarchy of exceptions, you should always start catching the more specific exceptions and finally catch the broader exceptions. The hierarchy is usually determined by Inheritance.

In your case since you are catching that exception last, it should be because the timeout exception you are catching first is refering to another package or namespace. And the last exception you are catching is a super class of the other exceptions.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • there isnt problem at hierrarchy, there isnt such an exception like timeout for bind method. ldap.TIMEOUT is for search method. i didnt know that. thank you by the way – kursat Jul 16 '11 at 11:39
1

If issubclass(type(raised), named_in_except_clause), the except clause will trigger. So if an exceptions that "is a" ldap.SERVER_DOWN is raised and it's none of the exceptions in the previous except clauses, the last except clause will trigger. The LDAP documentation doesn't seem to say anything about the hierachy of the LDAP-specific exceptions, but you could always explore it at the REPL.