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( ...

@rouven_bauer I am trying to get the exception using the try-except:

warnings.filterwarnings("ignore", category=ExperimentalWarning)
try:
driver.verify_connectivity()
doing a lot of interesting things here.....
except:
exit( f"Failed Neo4j connectivity test.")

If there is a connectivity, then do all the logic, if there is no connectivity, raise the driver.verify_connectivity() error:

---------------------------------------------------------------------------
ServiceUnavailable                        Traceback (most recent call last)
Input In [5], in <cell line: 1>()
      1 with warnings.catch_warnings():
      2     warnings.filterwarnings("ignore", category=ExperimentalWarning)
----> 3     driver.verify_connectivity()

File ~/Library/Python/3.8/lib/python/site-packages/neo4j/meta.py:85, in experimental.<locals>.f__.<locals>.f_(*args, **kwargs)
     83 from warnings import warn
     84 warn(message, category=ExperimentalWarning, stacklevel=2)
---> 85 return f(*args, **kwargs)

File ~/Library/Python/3.8/lib/python/site-packages/neo4j/__init__.py:445, in Neo4jDriver.verify_connectivity(self, **config)
    441 """
    442 :raise ServiceUnavailable: raised if the server does not support routing or if routing support is broken.
    443 """
    444 # TODO: Improve and update Stub Test Server to be able to test.
--> 445 return self._verify_routing_connectivity()

File ~/Library/Python/3.8/lib/python/site-packages/neo4j/__init__.py:471, in Neo4jDriver._verify_routing_connectivity(self)
    469     if val is not None:
    470         return routing_info
--> 471 raise ServiceUnavailable("Could not connect to any routing servers.")

ServiceUnavailable: Could not connect to any routing servers.

Thanks @rouven_bauer

I'm not sure I fully understand what you're trying to achieve. But if I understand you right, you want the potential error of verify_connectivity to propagate and skip all your work.

If so, you could do it like that:

def interesting_things_with_the_driver(driver):
    with driver.session(...) as session:
        ...  # do the interesting stuff here


def main():
    # the with block (context manager) takes care of closing the driver.
    with neo4j.GraphDatabase.driver(...) as driver:
        driver.verify_connectivity()  # raises if there's no connectivity
        interesting_things_with_the_driver(driver)


def alternative_main():
    driver = neo4j.GraphDatabase.driver(...)
    try:
        driver.verify_connectivity()  # raises if there's no connectivity
        # only gets executes if verify_connectivity succeeded
        interesting_things_with_the_driver(driver)
    finally:
        # regardless if we fail or not, the driver needs to be closed to free
        # all resources
        driver.close()