减少try的数量(python中除外)

减少try的数量(python中除外),python,try-except,Python,Try Except,我正在编写一个类似于诊断程序的程序,它运行一个测试,然后在此基础上进行更多的测试,所以大多数测试都是在“try”中完成的,除了有很多测试。除了减少尝试次数之外,还有其他方法可以实现这一点吗 下面是一个示例代码 try: treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all']) print "\n" print "Y

我正在编写一个类似于诊断程序的程序,它运行一个测试,然后在此基础上进行更多的测试,所以大多数测试都是在“try”中完成的,除了有很多测试。除了减少尝试次数之外,还有其他方法可以实现这一点吗

下面是一个示例代码

try:
    treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all'])
    print "\n"
    print "Your machine type is ",platform.machine()
        print "Compiling using default compiler\n"
    print treeinfo

except subprocess.CalledProcessError as e:
    print "ERROR\n"

try:
    with open ('helloworld.exe')as f:
        subprocess.call('helloworld.exe')
        print"Build success"
        log64 =  subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"])
        if arch64 in log64:
            print "Architecture of the compiled file is 64-bit "
        elif arch32 in log64:
            print "Architecture of the compiled file is 32-bit"
except IOError as e:
    print "Build failed\n"


print "\n"

上面相同的代码与不同的文件名重复,我知道这不是一个好的做法。我是python新手,谷歌搜索没有给出任何有用的结果。

您可以将逻辑拆分为单独的函数,并在try块中逐个调用它们:


其中捕获要处理的异常元组。

您可以将逻辑拆分为单独的函数,并在try块中逐个调用它们:


其中捕获要处理的异常元组。

执行它不需要打开helloworld.exe。另外,您希望程序在遇到错误时继续执行吗?我没有打开程序,我正在检查该程序是否存在,如果存在,然后打开它,是的,我希望程序执行下一个测试,即使该测试失败,并且该测试在最后打印结果,比如哪一个失败了,哪一个成功了。你正在打开程序。没有必要;如果程序不存在,subprocess.call将引发异常。因此,请不要尝试:将打开的“helloworld.exe”作为f:直接转到try:subprocess.call“helloworld.exe”?是的。Python库函数总是会优雅地失败,这是该语言设计理念的一部分。执行它不需要打开helloworld.exe。另外,您希望程序在遇到错误时继续执行吗?我没有打开程序,我正在检查该程序是否存在,如果存在,然后打开它,是的,我希望程序执行下一个测试,即使该测试失败,并且该测试在最后打印结果,比如哪一个失败了,哪一个成功了。你正在打开程序。没有必要;如果程序不存在,subprocess.call将引发异常。因此,请不要尝试:将打开的“helloworld.exe”作为f:直接转到try:subprocess.call“helloworld.exe”?是的。Python库函数总是会优雅地失败,这是该语言设计理念的一部分。据我所知,目前有14个try-catch异常,非常糟糕!,我将试一试。except子句的语法应该是except OSError、IOError、e,否则它将只捕获OSError实例并将它们绑定到名称IOError。此外,最好至少打印错误消息,如果任何步骤都依赖于前面步骤的结果,那么最好不要尝试进一步操作,如果是这样,最好的异常处理方案是根本不进行异常处理。据我所知,目前有14个try-catch异常,非常糟糕!,我将试一试。except子句的语法应该是except OSError、IOError、e,否则它将只捕获OSError实例并将它们绑定到名称IOError。此外,最好至少打印错误消息,如果任何步骤都依赖于前面步骤的结果,并且如果是这种情况,最好的异常处理方案是根本不进行异常处理,那么最好不要尝试进一步执行。
def a():
    treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all'])
    print "\n"
    print "Your machine type is ",platform.machine()
    print "Compiling using default compiler\n"
    print treeinfo

def b():
    subprocess.call('helloworld.exe')
    print"Build success"

def c():
    log64 =  subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"])
    if arch64 in log64:
        print "Architecture of the compiled file is 64-bit "
    elif arch32 in log64:
        print "Architecture of the compiled file is 32-bit"

def try_these(funs, catch):
    for fun in funs:
        try:
            fun()
        except catch:
            print 'ERROR!'

try_these([a, b, c], catch=(IOError, OSError))