Results 1 to 8 of 8

Thread: exception handling in python

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    exception handling in python

    Code:
    import time
    
    
    from sys import exit
    
    
    readable_data = []
    
    
    data_for_computations = []
    
    
    def GetCount():
        print "What is the current count?"
        count = raw_input(">")
        readable_data.append(time.ctime())
        readable_data.append(count)
        
        data_for_computations.append(time.time())
        data_for_computations.append(count)
        
        print readable_data
        print data_for_computations
        
    while 1 == 1:
        GetCount()
        
        try:
    
    
    
    
            print "The number of seconds elapsed since the last count is ",  int(time.time()) - int(data_for_computations[-4])
        except IndexError:
            continue
        finally:
            pass
    Why isnt the Index Error handled the first time the list is populated? The program crashes.....

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,450
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: exception handling in python

    How does it crash - what error message do you get ?
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Jan 2012
    Beans
    161

    Re: exception handling in python

    It raises the Index Error that the index is out of the list range....I know that it will be on program execution, because i have to get to a point where i have enough items in the list to compute line 27.........shouldnt the 'try/except' block handle that?

  4. #4
    Join Date
    May 2008
    Location
    UK
    Beans
    1,450
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: exception handling in python

    How many times does your code prompt you for the data ?
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  5. #5
    Join Date
    Jan 2012
    Beans
    161

    Re: exception handling in python

    Once. I ended up removing the try block

    I added:

    if len(data_for_computations) >= 4

    as a prelude to line 27 on....

  6. #6
    Join Date
    Jan 2012
    Beans
    161

    Re: exception handling in python

    where is the option to mark the thread as solved?

  7. #7
    Join Date
    Apr 2007
    Location
    Hamden, CT
    Beans
    649
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: exception handling in python

    Should be an option on the top tool bar called "Thread Tools"

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,163
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: exception handling in python

    Quote Originally Posted by LuisGMarine View Post
    Should be an option on the top tool bar called "Thread Tools"
    There is a problem whith this since they migrated to the new software. As a workaround you have to edit the title of the first post directly.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •