2012-05-03 13 views
0

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 
+1

裸の 'except'は使用しないでください。キャッチしようとしている型を宣言します。 – Daenyth

答えて

0
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 
4
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) 

。また、一般的なものをキャッチするのではなく、どんな例外を捕まえたいのかを具体的に説明する必要があります。

関連する問題