我正在尝试为我的应用程序编写一个错误处理类。每次都需要包含错误处理程序的完整路径吗?下面是我的代码。
appname/appname/model/error.py
class UserError(Exception):
""" User errors
"""
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
我的课堂功能:
from error import UserError
def doSomething(
""" Some function
"""
if (value == 2):
pass
else:
raise UserError('Value is not 2')
从我的应用程序中调用如下: from error import UserError
try:
print names['first']
except appname.model.error.UserError as e:
print e
升起时:
>> appname.model.error.UserError: 'No file specified'
我是否必须一直将其称为“appname.model.error.UserError”?或者有没有办法将此错误称为 UserError 甚至 error.UserError?我在哪里调整这个范围?如果我更改应用程序的目录结构(甚至名称),这似乎不是一个好主意,不是吗?