When is "raising a class instance" useful?

If you have an exception instance, why do you need to specify its class?

What is the advantage over just raise instance ?

+4
source share
3 answers

There is no advantage. raise Class, instance is an obsolete expression and is fully equivalent to raise instance . In particular, the first notation was removed in Python 3 and replaced by the last .

+2
source

There is no reason to actually use it. This archaic usage from the back before the exceptions was supposed to be class instances, and it went away in Python 3.

+2
source

You can pass one exception to another:

 exc = StopIteration('Iterator has already exhaused!') try: raise ValueError, exc except ValueError, e: print e.args[0] 

A good practical application of this still eludes me, although there must be some.

+1
source

Source: https://habr.com/ru/post/1344376/


All Articles