我有以下功能:无法赶上“错误”类型的异常
def findHardDriveLetter(drivename):
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
for drive in drives:
try:
volname = win32api.GetVolumeInformation(drive)[0].upper()
except:
pass
if volname == drivename.upper():
return drive
根据驱动状态,就会出现这种错误,我想我只是赶上了特定的错误:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<editor selection>", line 5, in findHardDriveLetter
error: (21, 'GetVolumeInformation', 'The device is not ready.')
使用type(exception).__name__
,错误被重新转换为类型错误。这似乎是从Python的错误类型的典型格式不同,如果我用
except error:
抓住它,我得到这个异常:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<editor selection>", line 20, in findHardDriveLetter
NameError: global name 'error' is not defined
那么,为什么这不是工作,我期待,以及如何在没有泛型除外的情况下捕获此异常?
http://stackoverflow.com/questions/ 2020014 /在python中获取完全限定类名称的对象 –
您可能会这样意思:'除非Exception作为错误:'除非您在某处定义了'error'。 – FamousJameous
奇怪。看看'type(exception).__ module__'。 – user2357112