请注意:-这个问题与语言无关。
我有一个用Python编写的代码库,其中有不同的层-数据库层,控制器层,实用程序功能层等。
现在,我要处理异常。
我认为you should ALWAYS wrap calls that can throw in try, catch blocks
是我的同事,他的看法不同。他说pass the exceptions higer up the call stack, and let them manage it
。
我的方法在代码中进行了解释:-
Views.py
def get_value(pk):
try:
ModelName.objects.get(pk=pk)
except Exception as e:
return None
def view(request, pk, *args, **kwargs):
product = get_product(pk)
if not product:
return
..
..
..
我同事的方法
Views.py
def get_value(pk):
return ModelName.objects.get(pk=pk)
def view(request, pk, *args, **kwargs):
try:
product = get_product(pk)
except Exception as e:
return
..
..
..
在辩护中,我说,在当前开发的代码段上不考虑异常处理是一个不好的借口,使异常处理成为其他人或以后的问题。
您对此有何看法?