pythonでのカスタム例外の練習

797 ワード

#      
a = 9
b = 0

try:
    s = a / b
    print(s)
except:
    print("     0")
else:
    print("    ")
finally:
    print("           ")


#         
print("        "*10)
a = 9
b = 0
try:
    print("           ")
    print("        :" % c)
    s = a / b
    print("%d %d     :%d"%(a,b,s))
# except:
#     print("          %d"%b)
except SyntaxError as msg:
    print("       ")
    print(msg)
except ZeroDivisionError as msg:
    print("         ")
    print(msg)
except NameError as msg:
    print("          ")
    print(msg)
else:
    print("%a %b         :%d,    "%(a.b,s))
finally:
    print("       ,  finally     ")

異常を書くときは、小さな範囲の異常を身近に書き、広い範囲の異常を下に書きます.