嵌套try的麻烦除了在python 3.3中

时间:2015-04-14 17:17:20

标签: python dictionary try-catch

我遇到的问题是,如果第一次尝试抛出异常,它似乎会将其视为except块下的所有try语句也返回异常。

if player.ClassName[0] != 'Undetermined':
    if (player.ClassName[1] != 'Undetermined') and (player.ClassName[2] != 'Undetermined'):
        for i in player.Abilities:
            try:
                OVERLIST[player.ClassName[0]][player.ClassName[1]][player.ClassName[2]]['Abilities'][i.name].process_ability(i)
            except:
                try: 
                    OVERLIST[player.ClassName[0]][player.ClassName[1]]['Abilities'][i.name].process_ability(i)
                except:
                    try:
                        OVERLIST[player.ClassName[0]]['Abilities'][i.name].process_ability(i)
                    except:
                        print("Ability: "+i.name+" not found in ability index for "+player.ClassName[0]+"/"+player.ClassName[1]+"/"+player.ClassName[2])
                        print("\tUsed by: "+player.name+"\t")
    elif player.ClassName[1] != 'Undetermined':
        for i in player.Abilities:
            try:
                OVERLIST[player.ClassName[0]][player.ClassName][i.name].process_ability(i)
            except:
                try:
                    OVERLIST[player.ClassName[0]]['Abilities'][i.name].process_ability(i)
                except:
                    print("Ability: "+i.name+" not found in ability index for "+player.ClassName[0]+"/"+player.ClassName[1])
                    print("\tUsed by: "+player.name+"\t")  
    else:
        for i in player.Abilities:
            try:
                OVERLIST[player.ClassName[0]]['Abilities'][i.name].process_ability(i)
            except:
                print("Ability: "+i.name+" not found in ability index for "+player.ClassName[0])
                print("\tUsed by: "+player.name+"\t")     

背景: 这是用于读取游戏创建的战斗日志的程序。

播放器是一个Player对象,其中包含一个名为Abilities的列表,其中包含所述播放器使用的所有能力,另一个列表ClassName为3,其中ClassName [0]是播放器基类的字符串名称,ClassName [1]为玩家的子类和ClassName [2]是玩家的规范(基本上是子类的子类)

OVERLIST是一个嵌套字典,其中键是基类的名称,值是包含子类键的字典加上一个名为“Abilities”的键,其值包含特定于该类的能力的字典。每个子类键具有一个字典的值,该字典具有用于子类的每个“spec”的键,以及具有所述子类的相关能力字典的“能力”键。每个spec键与它自己的“Abilities”键具有相同的方式,并且具有spec键的相关能力词典。

0 个答案:

没有答案