Instance Exception Oddity: Implicit and Explicit not the same?

rt lange whiteywidow at yahoo.com
Tue Dec 9 23:23:18 CET 2003


Peter Otten <__peter__ at web.de> wrote in message news:<br4cov$h7$01$1 at news.t-online.com>...

> "If it is an instance of the class, the *instance* *becomes* the *exception*
> *value*"
this says nothing about the *type* of exception, just the *value*.


"6.9 The raise statement
...The first two objects are used to determine the type and value of
the exception.
If the first object is an instance, the TYPE of the exception is the
class of the instance, the instance itself is the VALUE, and the
second object must be None."

>>> class E(Exception): pass

>>> i = E('foo')
>>> try:
	raise i
except:
	print sys.exc_info()[:2]

	
(<class __main__.E at 0x00A88090>, <__main__.E instance at
0x00A5A850>)

*makes perfect sense*

"If the first object is a class, it becomes the TYPE of the exception.
The second object is used to determine the exception value: If it is
an instance of the class, the instance becomes the exception VALUE."

>>> try:
	raise Exception, i #first object is a class: becomes the type
			       #second object instance of (sub)class: becomes the valu
except:
	print sys.exc_info()[:2]

	
(<class __main__.E at 0x00A88090>, <__main__.E instance at
0x00A5A850>)
>>> 
*hmmm "Exception" did NOT become the TYPE of the exception*

As for why I was passing an exception instance to the
constructor for a different exception...just pointing out the 
nonequivalence of the two forms.
Why would someone want to do this? Don't ask me.
I only use string exceptions. :)

RT




More information about the Python-list mailing list