Python中的错误处理可以检测程序状态

时间:2014-07-04 09:00:25

标签: python loops python-2.7 error-handling ipython

我是Python新手。我想为我的python脚本创建一个逻辑循环,当程序卡住它时会自动跳转到下一行并继续该程序。

以下是我的代码示例,它是一系列不同的参数值组合。我知道参数的某些组合可能会使程序崩溃,这就是为什么我想要一个错误处理循环来保持程序运行的原因。

   ini_file="model-simulation fL=0.1,fks=1,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)
   ini_file="model-simulation fL=0.1,fks=6,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)
   ini_file="model-simulation fL=0.1,fks=11,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)
   ini_file="model-simulation fL=0.1,fks=16,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)
   ini_file="model-simulation fL=0.1,fks=21,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)
   ini_file="model-simulation fL=0.1,fks=26,fno=1,fnc=1,fr=1,fs=1.ini"; pytopkapi.run(ini_file)

有时会出现如下图所示的错误消息,从而停止整个程序。有人可以帮帮我吗? enter image description here

1 个答案:

答案 0 :(得分:2)

嗯,你可以抓住例外,然后继续:

ini_files = ["model-simulation fL=0.1,fks=1,fno=1,fnc=1,fr=1,fs=1.ini",
             "model-simulation fL=0.1,fks=6,fno=1,fnc=1,fr=1,fs=1.ini"]
for ini in ini_files:
    try:
        pytopkapi.run(ini)
    except Exception as e:
        print(e)  # If you want to see your error

请参阅Error Handling文档