exception due to NoneType

Discussion in 'Python' started by Bruno Desthuilliers, Nov 7, 2009.

  1. asit a écrit :
    > In my program I want to catch exception which is caused by accessing
    > NoneType object.
    >
    > Can anyone suggest me how this can be done ??


    Not without the minimal working code exposing your problem, or the full
    traceback you got. Merely "accessing NoneType object" doesn't by itself
    raise any exception... I suspect you get the None object where you
    expected something else and try to access an attribute of this
    'something else', and ends up getting an AttributeError, but there are
    other possible scenarii that might fit your (very poor) description of
    the problem, so no way too help you without more informations. As a
    general rule, remember that the traceback is actually meant to *help*
    finding out what went wring.
    Bruno Desthuilliers, Nov 7, 2009
    #1
    1. Advertising

  2. Bruno Desthuilliers

    asit Guest

    In my program I want to catch exception which is caused by accessing
    NoneType object.

    Can anyone suggest me how this can be done ??
    asit, Nov 7, 2009
    #2
    1. Advertising

  3. Bruno Desthuilliers

    asit Guest

    On Nov 7, 10:36 pm, Bruno Desthuilliers
    <> wrote:
    > asit a écrit :
    >
    > > In my program I want to catch exception which is caused by accessing
    > > NoneType object.

    >
    > > Can anyone suggest me how this can be done ??

    >
    > Not without the minimal working code exposing your problem, or the full
    > traceback you got. Merely "accessing NoneType object" doesn't by itself
    > raise any exception... I suspect you get the None object where you
    > expected something else and try to access an attribute of this
    > 'something else', and ends up getting an AttributeError, but there are
    > other possible scenarii that might fit your (very poor) description of
    > the problem, so no way too help you without more informations. As a
    > general rule, remember that the traceback is actually meant to *help*
    > finding out what went wring.


    I could have described the error, but the problem is that it's
    dependent of a third party library..

    Let me write the code here...

    import twitter

    api = twitter.Api('asitdhal','swordfish')
    users = api.GetFriends()
    for s in users:
    print
    print "##########################################"
    try:
    print "user id : " + str(s.id)
    print "user name : " + s.name
    print "user location : " + s.location
    print "user description : " + s.description
    print "user profile image url : " + s.profile_image_url
    print "user url : " + s.url
    print "user status : " + str(s.status)
    except TypeError:
    pass


    look at the except TypeError. This is supposed to catch only exception
    thrown by NoneType.

    please help me.
    asit, Nov 7, 2009
    #3
  4. asit a écrit :
    > On Nov 7, 10:36 pm, Bruno Desthuilliers
    > <> wrote:
    >> asit a écrit :
    >>
    >>> In my program I want to catch exception which is caused by accessing
    >>> NoneType object.
    >>> Can anyone suggest me how this can be done ??

    >> Not without the minimal working code exposing your problem, or the full
    >> traceback you got. Merely "accessing NoneType object" doesn't by itself
    >> raise any exception... I suspect you get the None object where you
    >> expected something else and try to access an attribute of this
    >> 'something else', and ends up getting an AttributeError, but there are
    >> other possible scenarii that might fit your (very poor) description of
    >> the problem, so no way too help you without more informations. As a
    >> general rule, remember that the traceback is actually meant to *help*
    >> finding out what went wring.

    >
    > I could have described the error, but the problem is that it's
    > dependent of a third party library..


    This more often than not translates to "dependent of a wrong use of a
    3rd part library".

    > Let me write the code here...
    >
    > import twitter
    >
    > api = twitter.Api('asitdhal','swordfish')


    I hope this is not your actual login.

    > users = api.GetFriends()
    > for s in users:
    > print
    > print "##########################################"
    > try:
    > print "user id : " + str(s.id)


    Please learn to use print and string formatting. Here are two ways to
    get rid of the need to use str():

    1/ print "user id : %s" % s.id
    2/ print "user id : ", s.id


    > print "user name : " + s.name
    > print "user location : " + s.location
    > print "user description : " + s.description
    > print "user profile image url : " + s.profile_image_url
    > print "user url : " + s.url
    > print "user status : " + str(s.status)
    > except TypeError:
    > pass


    Silently passing exception is 99.999 out of 100 a very stupid thing to do.

    >
    > look at the except TypeError.


    Yes, I've seen it. It's stupid, and actually make debugging harder. Any
    statement in this try block could raise a TypeError if the looked up
    attribute of 's' is not a string. Python is NOT php, and will not
    happily adds apples and parrots - you can only concatenate strings with
    strings, not with ints or None or whatever...

    > This is supposed to catch only exception
    > thrown by NoneType.


    Please get your facts straight.
    1/ The NoneType doesn't "throw" (the appropriate python term is "raise")
    any exception by itself
    2/ this except clause will catch any TypeError. Try this:

    try:
    x = "aaa" + 42
    except TypeError, e:
    print "This has nothing to do with NoneType"


    > please help me.


    Help yourself and read the FineManual. Then make appropriate use of
    string formatting (your best friend for simple formatted outputs)
    instead of useless string concatenations.
    Bruno Desthuilliers, Nov 7, 2009
    #4
  5. > api = twitter.Api('asitdhal','swordfish')

    You just gave the world the account-information to your twitter-account.
    You'd rather change these asap, or somebody hijacks your account...

    Diez
    Diez B. Roggisch, Nov 7, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnaud-F. FAUSSE
    Replies:
    2
    Views:
    680
    Arnaud-F. FAUSSE
    Nov 27, 2003
  2. Fresh
    Replies:
    2
    Views:
    624
    Bo Persson
    Apr 22, 2008
  3. Jordan Harry
    Replies:
    1
    Views:
    470
    Sean DiZazzo
    May 2, 2008
  4. Poor Yorick
    Replies:
    0
    Views:
    451
    Poor Yorick
    Jun 14, 2009
  5. Terry Reedy
    Replies:
    4
    Views:
    982
    Terry Reedy
    Jun 17, 2009
Loading...

Share This Page