1

Our program does these kinds of operations hundreds of times for many different variables and lists, then uses them throughout the program:

variable = values[5]

The list values are coming from the command line, often as words, paragraphs, rows of a table or other (it's not important). Our issue is that our program, which is designed to run in a continuous loop, stops when there is an index out of range error. Since this comes from the command line, we can expect these fairly often, but cannot accept our program stopping.

Thus, the question is: How can I catch all index out of range errors in python so that my program does not stop. Do I have to use try/except statements everywhere I make the above type statements (which is a lot of extra work for 40k lines of code) or can I do a catch all somehow? How the error is handled is important (I expect 99% of the time we can set it to NULL, ERROR or 0), but keeping the loop running is even more important.

6
  • 7
    The list values are coming from the command line, often as words, paragraphs, rows of a table or other (it's not important). Why not just validate all your input data somehow, once, when you load the program? Am I missing something about this?
    – enderland
    Jan 19 '16 at 16:49
  • Data validation is a different subject, we are doing it. This is about data capture and access. We run a command and the response gets piped into a list, either using split function, psutil, or other python packages. So in the above example, I would say values[5] 'typically' means grab the 5th word from the command line output so I can do something with it. We may discover that the way we are parsing is incorrect for certain systems we install this on. Fine, we will fix it, but we cannot have the loop just stop. Do I need 10k try/excepts, or is there another way.
    – gunslingor
    Jan 19 '16 at 21:18
  • The question is pretty simple. I just want to take one action when index out of range occurs... assign the item that caused the error a NULL value and move on. I don't want to have to do try/except statements every time I do the above type operation to accomplish this, there are thousands. Here's an example of what I consider a catchall: "except (KeyboardInterrupt, SystemExit):". I want to do something like "except (IndexError, r): r=NULL, continue loop.... where r would be values[5] i this case.
    – gunslingor
    Jan 19 '16 at 21:27
  • 1
    Sounds like you want this: stackoverflow.com/questions/2492087/… Jan 20 '16 at 3:07
  • Yeah, those are the solutions I know about. But rather than repeating that everytime I use a list, I would much rather have a catch all for the entire program. I guess i'll end up just creating a function that returns the index value or NULL and use that instead of the above example... it just really seems like there should be a global way to address this problem, like the keyboard interrupt seems to do. If some other programmer comes behind me, he'll have to know to use the function. There has to be effective global error handling somehow in python.
    – gunslingor
    Jan 20 '16 at 5:04
2

There is no global solution that just sets the problem variable to NULL and continuous on with normal program flow. This appears to be the only way:

try:
   variable=values[5]
except:
   variable='error'

You'll have to change the original one liner to a 4 liner everywhere you use a list variable. It is the most appropriate way since it allows specific response for different variables, but it is a shame that your 10,000 line program is probably going to end up being 30,000 lines just to deal with an index out of range error. Furthermore, you cannot really use referenced lists in equations do to the lack of global error handling in python, which will bulk it up even more. For example:

string_var = "first name: " + values[5] + "last name: " + values[6]

Will not work for your program since you are not 110% certain what your lists will contain (only 99% certain). You'll have to rewrite this using multiple discrete exception handling for each list item, or one exception that has if statements for each discrete list item.

1
0

add this global function to your code

def set_var(value):
    if len(value) <= 5:
        return value[5]
    else:
        return "None"

then you can use it throughout your code.

variable = set_var(variable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.