【发布时间】:2015-11-13 09:13:10
【问题描述】:
一些 IO 操作会产生一些错误。重要的是,它不是一个例外,而是一组。所以,我们设置了套接字错误,设置了文件 io.不同io操作如何处理无交集的异常组?
例如,OSError 处理文件 io 错误和一些(?)套接字错误。
我只有一个解决方案:使用 try-except 包装 io 操作并引发用户定义的异常。
def foo():
try:
# some file io
except:
raise MyFileIOException(reason=sys.exc_info())
try:
# some socket io
except:
raise MySocketIOException(reason=sys.exc_info())
def bar():
try:
foo()
except MyFileIOException as exc:
# handle
except MySocketIOException as exc:
# handle
有没有更好更优雅的解决方案?
【问题讨论】:
标签: python error-handling exception-handling