[英]Suppress an Exception but Continue after the Exception, not after the Suppression
[英]Continue after exception
幫助我弄清楚當WMI無法連接到主機並轉到列表中的下一台計算機時,腳本將如何繼續運行。 除非之后我應該繼續使用嗎?
import wmi
MachineList = ["Computer1","Computer2","Computer3"]
try:
for machines in MachineList:
c = wmi.WMI(machines) # <---- Go to next in the for loop when connection fail???
for os in c.Win32_OperatingSystem():
print os.Caption
except:
pass
import wmi
MachineList = ["Computer1","Computer2","Computer3"]
for machines in MachineList:
try:
c = wmi.WMI(machines) # <---- Go to next in the for loop when connection fail???
for os in c.Win32_OperatingSystem():
print os.Caption
except Exception: #Intended Exception should be mentioned here
print "Cannot Connect to {}".format(machines)
一般而言,除非您將異常用於控制流,否則應盡快將其捕獲,以防止與其他異常混合。 另外,您應該具體說明要捕獲的異常,而不是捕獲通用的異常。
for machine in MachineList:
try:
c = wmi.WMI(machine)
for os in c.Win32_OperatingSystem():
print os.caption
except Exception:
print "Failed on machine %s" % machine
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.