class MyException(Exception):
def __str__(self):
return self.args[0]
def DoWork():
raise MyException("My hoverboard is full of eels")
pass
if __name__ == '__main__':
try:
DoWork()
except MyException as ex:
print("This will get printed: " + str(ex)) #Line1
print("This will not get printed and will raise exception: " + ex) #Line2
为什么异常需要在第 1 行中进行显式转换。理想情况下,它应该如第 2 行所示工作。注意:我已经尝试过 -从 MyException 类中删除str并添加str;他们都没有工作。请帮忙。