我很难找到一个好的数据源来了解如何处理和引发异常。辅助函数或类方法是否应该处理引发的问题,或者调用者处理捕获异常的逻辑是否是正常约定?
我们是否在两个地方都这样做并一直跟踪堆栈跟踪?
选项A:
def change_permissions(file):
os.chmod(file, 0o755)
try:
change_permissions('/Users/user/Documents/file.txt')
except Exception:
print('do something with this')
选项B:
def change_permissions(file):
try:
os.chmod(file, 0o755)
except EnvironmentError:
print('do something with this')
change_permissions('/Users/user/Documents/file.txt')
白板的微信
相关分类