- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当我使用以下用户定义的异常时,我收到一条警告说 BaseException.message 在 Python 2.6 中已被弃用:
class MyException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
这是警告:
DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
self.message = message
这有什么问题?为了消除弃用警告,我需要进行哪些更改?
最佳答案
只需从 Exception
继承你的异常类,并将消息作为第一个参数传递给构造函数
例子:
class MyException(Exception):
"""My documentation"""
try:
raise MyException('my detailed description')
except MyException as my:
print my # outputs 'my detailed description'
您可以使用 str(my)
或(不太优雅)my.args[0]
来访问自定义消息。
在较新版本的 Python(从 2.6 开始)中,我们应该从 Exception 继承我们的自定义异常类(starting from Python 2.5)从 BaseException 继承。背景在PEP 352中有详细描述.
class BaseException(object):
"""Superclass representing the base of the exception hierarchy.
Provides an 'args' attribute that contains all arguments passed
to the constructor. Suggested practice, though, is that only a
single string argument be passed to the constructor."""
__str__
和 __repr__
已经以有意义的方式实现,特别是对于只有一个 arg(可以用作消息)的情况。
您不需要重复 __str__
或 __init__
实现或创建 _get_message
其他人的建议。
关于python - 在 Python 2.6 中不推荐使用 BaseException.message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1272138/
我在这里错过了什么? import sys class MyBaseError(BaseException): def __init__(self, message, base_message
我正在制作一个自定义插件来查询数据库中的用户信息以帮助客户支持。我的后端是松弛的。 每次我启动 bot 命令时,我都会收到: Computer says nooo. See logs for deta
我正在处理一个项目,该项目涉及连接到远程服务器、等待响应,然后根据该响应执行操作。我们捕获了几个不同的异常,并且根据捕获的异常而表现出不同的行为。例如: def myMethod(address, t
这个错误是什么意思?我正在从 django.shortcuts import render, Http404, HttpResponseRedirect 导入 但是为什么我不能使用 HttpRespo
在执行下面的代码时,如果由于某种原因无法获取 firefox 配置文件/webdriver,我会收到以下错误消息: exceptions must be old-style classes or de
我想创建自己的 Python 异常类,如下所示: class MyException(BaseException): def __init__(self, errno, address):
这两者有什么区别: except: # do something 和 except BaseException as be: print(be) 我的意思是在第一种情况下所有可能的异常
以下是我的代码: test = 'abc' if True: raise test + 'def' 当我运行它时,它给了我 TypeError TypeError: exceptions mu
我知道 Exception 和有什么区别和 BaseException在 Python .我想知道什么是好的做法和更多的 Pythonic: 我的异常是否应该从 BaseException 继承或 E
我正在用线程和钩子(Hook)编写 Python 扩展模块。我需要从我的一个线程向 Python 主线程抛出异常。为此我使用 int PyThreadState_SetAsyncExc(unsigne
在 python 中,每当我们编写用户定义的异常时,我们都必须从类 Exception 扩展它。我的问题是为什么我们不能从 BaseException 扩展它,它是异常层次结构的父类(super cl
根据 python 文档,Exception 派生自 BaseExceptions,我应该将它用于用户定义的异常。所以我有: class VisaIOError(Exception): def
Python 2 documentation说“鼓励程序员从 Exception 类或其子类之一派生新的异常,而不是从 BaseException”。没有进一步解释原因。 我很好奇为什么会这样推荐?是
以下代码有什么问题(在 Python 2.7.1 下): class TestFailed(BaseException): def __new__(self, m): self
当我使用以下用户定义的异常时,我收到一条警告说 BaseException.message 在 Python 2.6 中已被弃用: class MyException(Exception):
当我使用 flask run 从终端运行 Flask 时,如果我的代码有错误,我会看到“TypeError:exceptions must derive from BaseException”而不是实
当我运行此代码时: i=0 while i<5: i=i+1; try: SellSta=client.get_order(symbol=Symb,orderId=Se
最近在 Pytest 中运行我的测试时,我开始在测试结果结束时收到一个奇怪的警告;以下内容的许多多次迭代: Exception ignored in: . at 0x10eea07a0> Traceb
我正在尝试测试与 Amazon Forecast 服务交互的代码块,它看起来与 https://github.com/aws-samples/amazon-forecast-samples/blob/
所以今天这让我困惑了太久。我把它扔给你们。 我得到了 TypeError: exceptions must be old-style classes or derived from BaseExcep
我是一名优秀的程序员,十分优秀!