如何在Python中引用异常类? [英] How to reference an exception class in Python?

查看:125
本文介绍了如何在Python中引用异常类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获一个 GPSException gpxpy库.

try:
    gpx = gpxpy.parse(open(filepath))
except GPXException:
    print "GPXException for %s." % filepath

由于我是Python的新手,所以我不明白如何通过gpxpy.gpx.GPSException之类的命名空间或诸如..

Since I am new to Python I do not understand how one would reference the exception via namespace such as gpxpy.gpx.GPSException or an import statement such as ..

import gpxpy
import gpxpy.gpx
import gpxpy.gpx.GPSException

推荐答案

您需要正确引用该异常.

You need to reference the exception correctly.

直接将异常导入模块中,或使用完整的参考文献:

Either import the exception directly into your module, or use the full reference:

import gpxpy.gpx

try:
    # ...
except gpxpy.gpx.GPSException:
    # ...

from gpxpy.gpx import GPSException

try:
    # ...
except GPSException:
    # ...

这篇关于如何在Python中引用异常类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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