我知道我可以在python中捕获异常,例如:
try: with open('file.log') as file: read_data = file.read() except: print('Exception!')
但是如何获取异常类型或错误消息呢?
try: with open('file.log') as file: read_data = file.read() except Exception as e: print(e)
必须将异常强制转换为变量。这个和更多是在 Python documentation