在python中:如何忽略quit()之外的错误



动机:我经常在动态测试代码(换句话说,在写代码的同时进行测试(。我很清楚前面的错误,但我只想运行代码直到我知道没有错误的quit()行,并忽略忽略quit((行之外的任何错误,这将在稍后处理。

不幸的是,quit()之外的任何错误都会阻止我运行代码。

我需要一种方式对蟒蛇说:;只需运行代码直到quit(),不必担心以后会发生什么";。

我怎样才能做到这一点?我对quit()没有特别的依恋。如果另一个功能也能实现同样的效果,那么这对我来说是可以接受的

不幸的是,调试器断点和quit((都不能解决"语法";或超出断点或quit((行的缩进错误。

最好的策略可能只是注释掉那些你不想处理的代码。

例如,以下代码在quit((行之外有缩进错误。如果不修复它,你将无法运行:

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)

运行时,您会遇到以下错误:

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

如果你在quit((之后注释掉了行,那么它会按照你想要的方式工作:

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)
'''

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)

最新更新