Python:想捕捉异常,但不知道它来自哪里? [英] Python: want to catch exception, but don't know where it comes from?

查看:310
本文介绍了Python:想捕捉异常,但不知道它来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django教程学习Python,我有这个代码

I'm learning Python using the Django tutorial, and I have this code

poll = Poll.objects.get(pk = poll_id)

当poll_id不在数据库中时,我得到一个DoesNotExist异常,这很好,因为该教程告诉我要执行以下操作:

When poll_id is not in the database I get a DoesNotExist exception, which is fine because the tutorial tells me to do the following:

try:
    poll = Poll.objects.get(pk = poll_id)
except Poll.DoesNotExist:
    raise Http404

我的问题是:if我没有帮助我的教程,下面的堆栈跟踪怎么知道DoesNotExist异常是Poll的一部分?

My question is: if I didn't have the tutorial to help me, how would I, given the stack trace below, have known that the DoesNotExist exception is part of Poll?

(即我的猜测是将'DoesNotExist:除外)告诉我:全局名称'DoesNotExist'未定义)

(i.e. my guess would have been to put 'except DoesNotExist:' and this would have told me: "global name 'DoesNotExist' is not defined")

Request Method: GET
Request URL: http://127.0.0.1:8000/polls/3/

Django Version: 1.6.2
Python Version: 2.7.2
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/tom/Documents/dev/django-test/mysite/polls/views.py" in detail
  26.   poll = Poll.objects.get(pk = poll_id)
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in get
  151.         return self.get_queryset().get(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
  307.                 self.model._meta.object_name)

Exception Type: DoesNotExist at /polls/3/
Exception Value: Poll matching query does not exist.

全新的python。我明显地缺少一些明显的东西。

Totally new to python. I'm obviously missing something obvious.

推荐答案

你不会知道;您必须查看项目文档或抛出异常的代码

You wouldn't have known; you'd have to look at the project documentation or the code throwing the exception.

请注意, Model.DoesNotExist 引用是一个方便,因此您不必导入异常。你不会从抛出异常的代码中推断出这一点。这个特殊的Django异常对象是 django.core.exceptions.ObjectDoesNotExist 根据模型自定义。

Note that the Model.DoesNotExist reference is a convenience so you don't have to import the exception either. You wouldn't deduce this from the code throwing the exception. This special Django exception object is a subclass of django.core.exceptions.ObjectDoesNotExist customized per model.

一般来说,您可以捕获异常并查看异常捕获的模块:

In general, you could catch Exception and look at the module of the exception caught:

except Exception as e:
    print type(e), type(e).__module__

这将告诉你什么模块定义了例外情况,您可以选择将来从中导入。

This would tell you what module the exception is defined in, giving you the option to import it from there in future.

这篇关于Python:想捕捉异常,但不知道它来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆