I have a section of code that gives me a urllib3 error when the IP address of the host it's connecting to is wrong, and also stops the App from starting up.
This connection attempt is in a view(not the main view), which I'm assuming is being called when the application starts up.
I want to write a try
to check for this exception, however, the error doesn't mention what exception type it's seeing.
Q: What Exception type can I specify in the try
to handle this error when then application starts up?
Error
Was unable to import app
Error: HTTPSConnectionPool(host='10.0.0.1', port=834): Max retries exceeded with url: /items (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 113] No route to host',))
Thanks in advance.
Thanks @cricket_007
solved it by importing the exceptions for requests.
from requests.exceptions import ConnectionError
and then trigger in the try
statement.
try:
<<something>>
except ConnectionError:
<<something>>
In answer to the question actually asked, you can find out the base class(es) of a Python class via its __bases__
tuple. For example,
>>> class MyZeroDivide( ZeroDivisionError):
... pass
>>> z = MyZeroDivide() # instance
>>> z.__class__
<class '__main__.MyZeroDivide'> # class
>>> z.__class__.__bases__
(<type 'exceptions.ZeroDivisionError'>,) # base class(es)
>>> z.__class__.__bases__[0].__bases__
(<type 'exceptions.ArithmeticError'>,) # up a level
>>> z.__class__.__bases__[0].__bases__[0].__bases__
(<type 'exceptions.StandardError'>,) # up two levels
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.