【发布时间】:2021-08-26 19:02:16
【问题描述】:
假设我有包含异常名称的字符串:
s1 = 'KeyError'
s2 = 'ArithmeticError'
s3 = 'OSError'
s4 = 'ZeroDivisionError'
.....
sn = 'SomeOtherError'
我需要做的是:
if issubclass(s4, (s1, s2, s3, sn)) == True:
print('You dont have to catch this exception because the parent is already caught')
由于某种原因,在这种情况下使用 globals() 没有帮助。由于我不是经验丰富的程序员,我只能猜测是因为那些是内置异常...
然而,我可以做些什么来完成我想要完成的事情?
任何建议将不胜感激!
【问题讨论】:
-
你有什么理由首先将异常作为字符串?
-
@MisterMiyagi 它们来自用户输入的字符串
标签: python string exception inheritance subclass