Possible Documentation bug: Chapter 8. Errors and Exceptions
Hello, I was going through the chapter as per the below link : https://docs.python.org/3/tutorial/errors.html#handling-exceptions The code shown is as below: class B(Exception): pass class C(B): pass class D(C): pass for cls in [B, C, D]: try: raise cls() except D: print("D") except C: print("C") except B: print("B") - Now as per the above code the statement written below confused me a little: *"A class in an except <https://docs.python.org/3/reference/compound_stmts.html#except> clause is compatible with an exception if it is the same class or a base class thereof (but not the other way around — an except clause listing a derived class is not compatible with a base class)."* - A class in the except clause is compatible with an exception if it is the same class: I agree to this. - However, the next part "or a base class thereof" confused me. Does this mean that if it is a base class (i.e a class which inherits from the exception class and is the base class of all the other classes C & D) only then will it be compatible? - Because if i reverse the order of the except clauses, the only prints would be 'B','B' and 'B'. I think this elaboration needs to be done. I would request you to provide your insights on this. Thanks and Regards, Rahul
Hi Rahul, thanks for asking,
I was going through the chapter as per the below link : https://docs.python.org/3/tutorial/errors.html#handling-exceptions [...] I think this elaboration needs to be done.
I would request you to provide your insights on this.
A simple way to think of it is to remember the exception hiearchy: https://docs.python.org/3/library/exceptions.html?highlight=baseexception#ex... For example, here, ArithmeticError inherits Exception, it's more specific, and ZeroDivisionError inherits from ArithmeticError, is really specific. So we have something like: class ArithmeticError(Exception): pass class ZeroDivisionError(ArithmeticError): pass expecting Exception will catch Exception, ArithmeticError and ZeroDivisionError expecting ArithmeticError will catch ArithmeticError and ZeroDivisionError expecting ZeroDivisionError will only catch ZeroDivisionError. Back to the example with B, C, D: D is the most specific, and B the most generic exception, so writing: except D: print("D") except C: print("C") except B: print("B") tries the most specific first, which have a chance to catch D. writing it the other way around, we're getting "except B" first, the generic one, which will catch B, C, and D, thus printing B three times. Hope it helps, Bests, -- Julien Palard https://mdk.fr
Hello Julien, Thank you for the clarification. I appreciate your help. Best Regards, Rahul On Tue, 12 Mar 2019, 03:47 Julien Palard, <julien@palard.fr> wrote:
Hi Rahul, thanks for asking,
I was going through the chapter as per the below link : https://docs.python.org/3/tutorial/errors.html#handling-exceptions [...] I think this elaboration needs to be done.
I would request you to provide your insights on this.
A simple way to think of it is to remember the exception hiearchy: https://docs.python.org/3/library/exceptions.html?highlight=baseexception#ex...
For example, here, ArithmeticError inherits Exception, it's more specific, and ZeroDivisionError inherits from ArithmeticError, is really specific. So we have something like:
class ArithmeticError(Exception): pass
class ZeroDivisionError(ArithmeticError): pass
expecting Exception will catch Exception, ArithmeticError and ZeroDivisionError expecting ArithmeticError will catch ArithmeticError and ZeroDivisionError expecting ZeroDivisionError will only catch ZeroDivisionError.
Back to the example with B, C, D: D is the most specific, and B the most generic exception, so writing:
except D: print("D") except C: print("C") except B: print("B")
tries the most specific first, which have a chance to catch D.
writing it the other way around, we're getting "except B" first, the generic one, which will catch B, C, and D, thus printing B three times.
Hope it helps,
Bests, -- Julien Palard https://mdk.fr
participants (2)
-
Julien Palard
-
Rahul Babtiwale