mercurio
mercurio

Reputation: 43

In python: how to ignore errors beyond quit()

Motivation: often I'm testing code on the fly (testing as I write the code, in other words). I am well aware of errors that lie ahead, but I only want to run the code up until the quit() line which I know has no errors, and ignore the ignore any errors beyond the quit() line, which will be dealt with later.

Unfortunately, any errors beyond quit() prevent me from running the code at all.

I need a way to say to python, "just run the code up until quit() and don't worry about what comes later".

How can I achieve this? I have no particular attachment to quit(). If another function would achieve the same, then that is acceptable to me.

Upvotes: 0

Views: 170

Answers (1)

Paul Wang
Paul Wang

Reputation: 1759

Unfortunately, neither debugger breakpoint nor quit() can solve the "syntax" or indentation error beyond the breakpoint or quit() line.

The best strategy may be just comment out those code you don't want to deal with.

For example, the following code has indentation error beyond quit() line. You won't be able to run without fixing it:

x,y = 1,2
z = x+y
print(z)
quit()
#code you don't want to deall with for now
#and my have bugs in it below
if t > 0 :
print(t)

When run it, you will encounter following error:

  File "/home/paul/test/quit.py", line 8
    print(t)
    ^
IndentationError: expected an indented block

If you comment out the lines after quit(), then it will work as you want:

x,y = 1,2
z = x+y
print(z)
quit()
#code you don't want to deall with for now
#and my have bugs in it below
'''
if t > 0 :
print(t)
'''

or

x,y = 1,2
z = x+y
print(z)
quit()
# #code you don't want to deall with for now
# #and my have bugs in it below
# if t > 0 :
# print(t)

Upvotes: 1

Related Questions