Exceptions should be old-style classes or derived from BaseException, not NoneType

When executing the code below, I get below errors if for some reason I can’t get the Firefox / webdriver profile:

exceptions should be old-style classes or derived from BaseException, not 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 
+6
source share
2 answers

This is because you are using raise without providing an exception or instance type.

According to the documentation :

The only argument to raise indicates an exception. This must be either an instance of an exception or an exception class (a class that derives from an 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 with no arguments can be used inside the except block to except exception.


FYI, in python3 instead it will be:

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

Note that raise with no argument allowed if you are in a catch with an exception handled:

If you need to determine if an exception was raised, but was not designed to handle it, a simpler form of the raise statement allows you to re-create the exception:

 >>> 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 the "Correcting Exceptions" in the documentation.)

Beware, however, that if the method called in the expect block clears the exception information, raise with no argument will cause the exception exceptions must be… repeated exceptions must be… Thus, explicitly assigning an exception to an exception except … as variable is safer:

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

Source: https://habr.com/ru/post/979487/


All Articles