Yes, you can use a try/except clause, e.g.:
In [1]: import networkx as nx
In [2]: G=nx.Graph()
In [3]: G.add_nodes_from([1,2,3])
In [4]: nx.dijkstra_path(G,1,2)
---------------------------------------------------------------------------
NetworkXNoPath Traceback (most recent call last)
/home/aric/<ipython console> in <module>()
...networkx/algorithms/shortest_paths/weighted.pyc in dijkstra_path(G,
source, target, weight)
74 return path[target]
75 except KeyError:
---> 76 raise nx.NetworkXNoPath("node %s not reachable from
%s"%(source,target))
77
78
NetworkXNoPath: node 1 not reachable from 2
In [5]: try:
...: nx.dijkstra_path(G,1,2)
...: except nx.NetworkXNoPath:
...: print "no path" # use pass to do nothing
...:
...:
no path
Aric