在哪里用try-except包装函数更好?

时间:2018-09-24 13:02:48

标签: python

下一个功能将因某些数据而失败。用try-except块将其包装在哪里更好?

 if attr in obj_request:
     new_record = GosReestr()
     new_record.display_name_department = request.user.profile_ad.department
     new_record.set_data(**obj_request[attr][_main_data])
     print('before')
     new_record.save()
     print('after')

这样好吗?

 if attr in obj_request:
     new_record = GosReestr()
     new_record.display_name_department = request.user.profile_ad.department
     new_record.set_data(**obj_request[attr][_main_data])
     print('before')
     try:
          new_record.save()
     except as e
     print('after')

还是将所有内容包装在一个if块中更好?

0 个答案:

没有答案