[Python-ideas] exception instantiation philosophy and practice [was: Let try-except check the exception instance]

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jun 1 01:54:11 EDT 2018


Ethan Furman wrote:

> Why is this?  Doesn't the exception have to be instantiated at some 
> point, even if just to print to stderr?

If it gets caught by an except clause without an else clause,
in theory there's no need to instantiate it.

However, Python doesn't currently seem to take advantage of
that:

 >>> class E(Exception):
...  def __init__(self, *args):
...   Exception.__init__(self, *args)
...   print("E got instantiated!")
...
 >>> try:
...  print("Trying")
...  raise E
... except E:
...  print("Caught an E")
...
Trying
E got instantiated!
Caught an E

-- 
Greg




More information about the Python-ideas mailing list