Python driver initial error not catchable?

Should this work to detect errors with the initial driver setup? If the database is not up, it is not caught at this point, and the Python program fails and shows all the correct messages.

try:
 driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth( ...
except:
 exit( ...

Yet Python is able to catch errors if the next step fails for some reason, and I can get control:

try:
 session = driver.session()
except:
 exit( ...

I am using the Java driver. I am able to create the driver and a session without the database being available. I get the exception below after a timeout period when trying to access an unavailable database through a transaction from the session.

org.neo4j.driver.exceptions.ServiceUnavailableException: Unable to connect to localhost:7687, ensure the database is running and that there is a working network connection to it.

Depending on the driver version you are using, this will not work. Try using driver.verify_connectivity(). There is also no need to wrap that in a try just to call exit in the except block. Should the driver fail to establish a connection (during the verify_connectivity call), it will raise en exception and that will terminate the interpreter if not caught. Additionally, you get the benefit of not masking the actual cause of the failure by just silently exiting the program.

That worked, except it displayed

ExperimentalWarning: The configuration may change in the future.
  driver.verify_connectivity()

In the API documentation I found this method of suppressing it:

import warnings
from neo4j import ExperimentalWarning
warnings.filterwarnings("ignore", category=ExperimentalWarning)

I changed my code to this:

driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth(...

try:
 driver.verify_connectivity()
except:
 exit( f"{logtime()} ... Failed Neo4j connectivity test.")

# Should be good. Cannot 'try' driver.session itself.
session = driver.session()

I thought it would be good to use an "except ExperimentalWarning:" block to suppress just that warning in just that one place in my code, but I could not get any variation of that to work. So the global suppression of experimental will do.

Thank you so much!

Glad I could help.

You can suppress warnings locally like so:

import warnings
from neo4j import ExperimentalWarning

driver = ...

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=ExperimentalWarning)
    driver.verify_connectivity()

It's a good idea I suppose because that way you'll notice should you come across other experimental API parts.