2

I have just learned about exception chaining in Python. What I see is, that there are two ways - inplicit and explicit. If I don't do anything special and raise caught exception. Py will automatically save info about previous exception (if I understand it correctly to __context__ attribute). If the second exception is not caught I'll get nice stack containing both exceptions. I can also make this explicitly using from keyword. In this case only difference I see is, that instead of using __context__ Py uses __cause__. And stack message changes from During handling of the above exception, another exception occurred: to The above exception was the direct cause of the following exception:. So why is there explicit exception chaining? If I don't do anything, I'll get the same thing.

  • You should use `__cause__` not `__context__` so that's not a real difference. The `raise Exception from None` syntax is just syntactic sugar I think, but more concise – Chris_Rands Aug 15 '18 at 13:39
  • relevant https://stackoverflow.com/questions/15123137/how-do-i-gracefully-include-python-3-3-from-none-exception-syntax-in-a-python-3 – Chris_Rands Aug 15 '18 at 13:39
  • Well, I'm not using `__context__` python is (if I don't use explicit chaining). So explicit chaining is just more consise way to write the same thing? Still don't get why I would need this. Chaining happens anyway. I don't see any real benefit of writing `... from err`. – user3883586 Aug 22 '18 at 11:39
  • IMO the difference is the same as between **test error** and **test failure**. Implicit chaining can be considered as error in exception handler and explicit tells you about uncaught exception. The net effect is that tests doesn't pass and your code stops execution, but the reasons are different. – WloHu Nov 19 '18 at 17:58

0 Answers0