0

Is there any way to return back and repeat the instruction that was handling an exception in Python?

E.g. if we get some data by input() method, and for some reason is caused an exception (e.g. when trying to convert the input string into int), we raised the exception, but after the exception, I would like again to go to the same line where the input() is.

Just note, "continue" is not an option, even if it is in a loop, because it could be several different input() assigning them to a different variables in different parts of the loop.

So the question again is:

while 1:
    try:
        foo = int(input(">")
        ...some other code here...
        bar = int(input(">")
        ...some other code here...
        fred = int(input(">")
        ...some other code here...

    except Exception:
        ... do something for error handling and ...
        jump_back_and_repeat_last_line_that_caused_the_exception

Imagine that the above code could be in a loop, and the exception can be caused in any instruction (foo... bar... fred...etc, or even can be any other line). So, if it fails in the "bar" line, it should try again the "bar" line.

Is there any reserved word to do this in python?

Borja Tarraso
  • 863
  • 2
  • 12
  • 20

3 Answers3

3

Define a function; Handle exception there.

def read_int():
    while 1:
        try:
            value = int(input('>'))
        except ValueError:
            # Error handling + Jump back to input line.
            continue
        else:
            return value

while 1:
    foo = read_int()
    bar = read_int()
    fred = read_int()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • This is good solution indeed. But what if the rest of lines inside the while are not just read_int()? like open() etc that could cause other exceptions. Of course I can move every single line to a method, with their own exception handling, with a while inside etc. But I was looking for something more like this: http://stackoverflow.com/questions/4298108/python-inspect-where-an-exception-raise-would-go – Borja Tarraso Feb 02 '14 at 17:53
2

There might be a way to do that, but it will probably result with a very poor design.

If I understand you correctly, then your problem is with the exception caused by calling input.

If that is indeed the case, then you should simply implement it in a separate method, which will handle the exception properly:

foo = getUserInput()
...some other code here...
bar = getUserInput()
...some other code here...
fred = getUserInput()
...some other code here...

def getUserInput():
    while 1:
        try:
            return int(input(">"))
        except Exception:
            pass
barak manos
  • 29,648
  • 10
  • 62
  • 114
1

don't do nothing in except:

while 1:
    try:
        a=int(raw_input('input an integer: ')) #on python2 it's "raw_input" instead of "input"
        break
    except ValueError, err:
        # print err
        pass
print 'user input is:', a

output is:

D:\U\ZJ\Desktop> py a.py
input an integer: a
input an integer: b
input an integer: c
input an integer: 123
user input is: 123
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108