继续检查所有实例版本,甚至一个在 python 中引发错误?

continue to check all instances version even one raise an error in python?

我有一个函数,我试图在其中获取数据库的版本's.i 有多个实例并且我能够连接所有 them.Problem 我面临的任何数据库由于某种原因无法连接或命令失败,它不会继续到下一个 db.All 我希望它继续到最后一个实例

def dbversion(user_suffix=None):
    try:
        db_name_list = abc.getdbstring(env_name=None)
        for dbname in db_name_list:
            connection = Connection(dbname)
            cursor = connection.cursor()
            version = connection.version
            print(" " + version + " ")
    except:
        print("Fail")

您可以将 try 块放在循环中:

def dbversion(user_suffix=None):
    db_name_list = abc.getdbstring(env_name=None)
    for dbname in db_name_list:
        try:
            connection = Connection(dbname)
            cursor = connection.cursor()
            version = connection.version
            print(" " + version + " ")
        except:
            print("Fail")