exception arg

Hrvoje Niksic hniksic at iskon.hr
Tue Jan 4 12:47:02 EST 2000


mn at altern.org (Mirko Nasato) writes:

> 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...

`arg' is an exception object, not a string.  Try this:

... ...
... except ZeroDivisionError, arg:
...     pass
...
>>> arg
<exceptions.ZeroDivisionError instance at 804b568>

The source of your confusion is that `arg' *prints* as a mere string:

>>> print "%s" % arg
integer division or modulo



More information about the Python-list mailing list