exception arg

Paul Robinson paul.robinson at quantisci.co.uk
Tue Jan 4 12:58:33 EST 2000


Mirko Nasato wrote:
> 
> I can't understand the following:
> 
> >>> 1/0
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ZeroDivisionError: integer division or modulo
> >>> try:
> ...     1/0
> ... except ZeroDivisionError, arg:
> ...     if arg == "integer division or modulo":
> ...             print "This is what I expect."
> ...     else:
> ...             print 'Unexpected. "arg" is "%s".' % arg
> ...
> Unexpected. "arg" is "integer division or modulo".
> >>>
> 
> The same seems to occur with every kind of exception, so maybe
> there's an explanation...

There is: arg is not a string....

For example:
PythonWin 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on
win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Portions Copyright 1994-1999 Mark Hammond (MHammond at skippinet.com.au)
>>> try:
... 	1/0
... except ZeroDivisionError, arg:
... 	print type(arg)
...     print `arg`
...     print arg
...     print str(arg) == 'integer division or modulo'
... 	
<type 'instance'>
<exceptions.ZeroDivisionError instance at 1137370>
integer division or modulo
1
>>> 

arg is an instance of an exception object.

The string that you were comparing it with is the string representation
of that object.

Hope that helps,
	Paul.




More information about the Python-list mailing list