<｜begin▁of▁sentence｜>### Instruction:
If you have a Python function that is intended to work only with undirected graphs, how do you ensure that it raises an exception when called with a directed graph or a multigraph?
### Response:
import networkx as nx

@nx.utils.not_implemented_for('directed')
def only_for_undirected_graphs(G):
    if not G.is_directed():
        print('This function can be applied.')
    else:
        print('This function cannot be applied to directed graphs.')

# Create an undirected graph
G = nx.Graph()

# Add some edges
G.add_edge(1, 2)
G.add_edge(2, 3)

# Try the function
only_for_undirected_graphs(G)

# Create a directed graph
D = nx.DiGraph()

# Add some edges
D.add_edge(1, 2)
D.add_edge(2, 3)

# Try the function, should raise an exception
try:
    only_for_undirected_graphs(D)
except nx.NetworkXNotImplemented:
    print('Function not implemented for directed graphs')
<|EOT|>
