Django-尽管有子功能404,功能仍继续执行

时间:2019-02-25 00:19:32

标签: python django exception exception-handling

#A运行时

def add_particle_to_atom(particle_uid, atom_uid):

  node_data = find_node_by_uid(particle_uid) #A
  particle = node_data['queryset'] #B

它触发了我认为会在REPL中导致异常的功能。

def find_node_by_uid(node_uid):
  # ...
  try:
    # thing will fail because i'm giving it garbage for a uid
  except: 
    print("|--- ERROR: there is no particle with that uid.")
    return HttpResponseNotFound("404")

但是控制台给出了与#B相关的错误。

>>> add_particle_to_atom('wefjhwljefh',a)
|--- CHECK: if particle with uid 'wefjhwljefh' exists.
|--- ERROR: there is no particle with that uid.
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/layne/Desktop/AlchDB/alchemy/views.py", line 258, in add_particle_to_atom
    particle = node_data['queryset']
  File "/Users/layne/Desktop/venv_alchdb/alchdb/lib/python3.7/site-packages/django/http/response.py", line 145, in __getitem__
    return self._headers[header.lower()][1]
KeyError: 'queryset'

当我告诉404子功能时,为什么这样做呢?而且我知道它失败了,因为print正在运行。我不想退出,因为我仍然希望REPL可用。

更新:如果我将#A的返回值放在打印之前,它将跳过打印并直接返回执行主要功能。

0 个答案:

没有答案