
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