[Python-bugs-list] [ python-Bugs-675928 ] Have exception arguments keep their type

SourceForge.net noreply@sourceforge.net
Tue, 28 Jan 2003 12:57:20 -0800


Bugs item #675928, was opened at 2003-01-27 22:06
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=675928&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Brett Cannon (bcannon)
Assigned to: Nobody/Anonymous (nobody)
Summary: Have exception arguments keep their type

Initial Comment:
If you execute the following code::

try:
    raise Exception('a string')
except Exception, err:
    print type(err)

it prints out that ``err`` is a type 'instance'.  It would be nice if it returned the type of the actual argument (in this case, type 'str').

----------------------------------------------------------------------

>Comment By: Brett Cannon (bcannon)
Date: 2003-01-28 12:57

Message:
Logged In: YES 
user_id=357491

I must be misunderstanding how exceptions handle passed-in values at instantiation.  I know you can pass in multiple values (I assume it's ``def __init__(*args, **kwargs)`` for Exception), but I was not expecting an instance type.  I think I was expecting the values from ``.args`` to be unpacked and stored in the variables instead of an actual exception object to returned in there with a smart ``__str__`` method.  Perhaps the docs should clarify that you receive actual copies of the exception object with its ``args`` attribute set to the argument?  The tutorial, as-is, says "the except clause may specify a variable after the exception name (or list) to receive the argument's value" which suggests to me that I will get the physical argument I passed into the exception; I expected tuple unpacking of the ``args`` attribute.

In other words I see why it is the way it is, but perhaps we should clarify in the tutorial that it is an exception instance storing the arguments and not the arguments themselves?

And I actually used ``str(err)`` to get my code to work, although now that I know that it actually is an instance of an exception it really isn't needed since I will just test for an exception.  =)

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-01-28 07:52

Message:
Logged In: YES 
user_id=31435

I'm a bit baffled by this, Brett:  when you instantiate a class, 
you can pass any number of arguments to its constructor.  
You happened to pass a single string argument when building 
an instance of Exception here, but you could have passed 
any number of arguments.  Why should the first argument be 
special?  (Or maybe you think the last argument should be 
special <wink>).  Or what if you didn't pass any arguments at 
all?

Having "the detail" bound to the instance object raised is a 
feature, and a major one.

If you're in the habit of passing a single string to Exception 
directly, note that you can already get it back via doing

    str(err)

in the "except" clause.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=675928&group_id=5470