Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果程序在Python3中失败,则跳到脚本末尾_Python_Python 3.x_Exception_Exception Handling_Controls - Fatal编程技术网

如果程序在Python3中失败,则跳到脚本末尾

如果程序在Python3中失败,则跳到脚本末尾,python,python-3.x,exception,exception-handling,controls,Python,Python 3.x,Exception,Exception Handling,Controls,我有一个脚本,用于在实验过程中控制一些仪器,如下所示: camera = Camera() stage = Stage() results = [] # loads of other initialization for n in steps: stage.move(n) img = camera.capture() # loads of other function/method calls results.append(img) results = np.

我有一个脚本,用于在实验过程中控制一些仪器,如下所示:

camera = Camera()
stage = Stage()
results = []
# loads of other initialization

for n in steps:
    stage.move(n)
    img = camera.capture()
    # loads of other function/method calls
    results.append(img)

results = np.array(results)
np.savetxt('data.txt',results)

camera.close()
stage.close()
如果回路内部发生异常,例如摄像机或舞台出现硬件问题,则我希望保存结果并关闭仪器。如果在循环之前发生异常,那么我只想关闭仪器。如何做到这一点?我可以使用许多try/except语句,但是还有其他更好的方法吗?

您可以使用try-finally语句

camera = Camera()
stage = Stage()
try:
  # do stuff
finally:
  camera.close()
  stage.close()
您可以使用try finally语句

camera = Camera()
stage = Stage()
try:
  # do stuff
finally:
  camera.close()
  stage.close()

你有几个选择。您可以根据需要首先注册处理程序,一个用于关闭仪器的处理程序,然后在循环之前注册一个用于保存结果的处理程序。不过,嗯

使用两个try/except:

try:
    camera = Camera()
    stage = Stage()
    results = []
    # loads of other initialization

    try:
        for n in steps:
            stage.move(n)
            img = camera.capture()
            # loads of other function/method calls
            results.append(img)
    finally:
         results = np.array(results)
         np.savetxt('data.txt',results)
finally:
    camera.close()
    stage.close()
也许:

try:
    do_save = False
    camera = Camera()
    stage = Stage()
    results = []
    # loads of other initialization

    do_save = True
    for n in steps:
        stage.move(n)
        img = camera.capture()
        # loads of other function/method calls
        results.append(img)
finally:
    if do_save:
        results = np.array(results)
        np.savetxt('data.txt',results)
    camera.close()
    stage.close()

你有几个选择。您可以根据需要首先注册处理程序,一个用于关闭仪器的处理程序,然后在循环之前注册一个用于保存结果的处理程序。不过,嗯

使用两个try/except:

try:
    camera = Camera()
    stage = Stage()
    results = []
    # loads of other initialization

    try:
        for n in steps:
            stage.move(n)
            img = camera.capture()
            # loads of other function/method calls
            results.append(img)
    finally:
         results = np.array(results)
         np.savetxt('data.txt',results)
finally:
    camera.close()
    stage.close()
也许:

try:
    do_save = False
    camera = Camera()
    stage = Stage()
    results = []
    # loads of other initialization

    do_save = True
    for n in steps:
        stage.move(n)
        img = camera.capture()
        # loads of other function/method calls
        results.append(img)
finally:
    if do_save:
        results = np.array(results)
        np.savetxt('data.txt',results)
    camera.close()
    stage.close()
可能的重复可能的重复