简体   繁体   English

异常必须是旧式类或派生自BaseException,而不是NoneType

[英]exceptions must be old-style classes or derived from BaseException, not NoneType

On executing below code I get below error if it fails to get firefox profile/webdriver for some reason: 在执行下面的代码时,如果由于某种原因导致无法获取firefox配置文件/ webdriver,则会出现以下错误:

exceptions must be old-style classes or derived from BaseException, not NoneType 异常必须是旧式类或派生自BaseException,而不是NoneType

I want to understand why this error is displayed in this case: 我想了解为什么在这种情况下会显示此错误:

self.error = 0  
self.profile, profileErrStatus = self.GetFireFoxProfile(path)
if self.profile:
  self.driver, driverErrStatus = self.GetFireFoxWebDriver(self.profile)
  if self.driver:
  else:
    print('Failed to get Firefox Webdriver:%s'%(str(sys.exc_info()[0])))
    raise
else:
  print('Failed to get Firefox Profile:%s'%(str(sys.exc_info()[0])))
  raise   

This is because you are using raise without providing an exception type or instance. 这是因为您在不提供异常类型或实例的情况下使用raise

According to the documentation : 根据文件

The sole argument to raise indicates the exception to be raised. 提出的唯一论据表明要提出的例外。 This must be either an exception instance or an exception class (a class that derives from Exception). 这必须是异常实例或异常类(派生自Exception的类)。

Demo: 演示:

>>> raise
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType

>>> raise ValueError('Failed to get Firefox Webdriver')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Failed to get Firefox Webdriver

Note that raise without arguments can be used inside an except block to re-raise an exception. 需要注意的是raise不带参数可以内部的使用except块重新抛出异常。


FYI, on python3, it would raise a RuntimeError instead: 仅供参考,在python3上,它会引发一个RuntimeError

>>> raise
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: No active exception to reraise

Note that raise without an argument is allowed if you are in a catch block with an exception currently handled: 请注意,如果您处于当前处理异常的catch块中, 允许不带参数的raise

If you need to determine whether an exception was raised but don't intend to handle it, a simpler form of the raise statement allows you to re-raise the exception: 如果您需要确定是否引发了异常但不打算处理它,则可以使用更简单的raise语句形式重新引发异常:

>>> try:
...     raise NameError('HiThere')
... except NameError:
...     print 'An exception flew by!'
...     raise
...
An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
NameError: HiThere

(From Raising Exceptions in the documentation.) (摘自文档中的例外情况 。)

Beware, though, that if a method called in the expect block clears the exception info, raise without an argument will result in the exceptions must be… exception again. 但要注意,如果expect块中调用的方法清除了异常信息,则不带参数的raise将导致exceptions must be…再次exceptions must be… exception。 So explicitly assigning the exception to a variable with except … as is safer: 所以明确地将异常分配给变量, except … as更安全:

try:
    raise NameError('HiThere')
except NameError as e:
    log_and_clear_exception_info('An exception flew by!')
    raise e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常必须是旧式类或派生自BaseException,而不是HttpResponseRedirect - exceptions must be old-style classes or derived from BaseException, not HttpResponseRedirect TypeError:exception必须是旧式类或派生自BaseException,而不是NoneType - TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType TypeError:异常必须是旧式类或从BaseException派生,而不是str - TypeError: exceptions must be old-style classes or derived from BaseException, not str TypeError:exceptions必须是旧式类或从BaseException派生,而不是str - TypeError:exceptions must be old-style classes or derived from BaseException, not str TypeError:异常必须是旧式类或派生自BaseException,而不是MagicMock - TypeError: exceptions must be old-style classes or derived from BaseException, not MagicMock django crash:异常必须是旧式类或派生自BaseException,而不是str - django crash: exceptions must be old-style classes or derived from BaseException, not str Python - 继承旧式类 - Python - inheriting from old-style classes 异常必须从 BaseException 派生 - Exceptions must derive from BaseException Pytest &amp; 自定义异常:TypeError:异常必须从 BaseException 派生,而不是 class 'module' - Pytest & custom exception : TypeError: exceptions must be derived from BaseException, not class 'module' 具有旧式类的Python描述符 - Python descriptors with old-style classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM