Exceptions

BGL algorithms throw these exceptions when preconditions are not met.

Defined in: <boost/graph/exception.hpp>

Example

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/exception.hpp>
#include <iostream>
#include <vector>

int main() {
    using namespace boost;
    using Graph = adjacency_list<vecS, vecS, directedS>;

    Graph g(3);
    add_edge(0, 1, g);
    add_edge(1, 2, g);
    add_edge(2, 0, g);  // creates a cycle

    std::vector<int> order;
    try {
        topological_sort(g, std::back_inserter(order));
    } catch (const not_a_dag& e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}
Exception: The graph must be a DAG.

Exception classes

struct bad_graph : public std::invalid_argument {
    bad_graph(const std::string& what_arg);
};
struct not_a_dag : public bad_graph {
    not_a_dag();
};
struct negative_edge : public bad_graph {
    negative_edge();
};
struct negative_cycle : public bad_graph {
    negative_cycle();
};
struct not_connected : public bad_graph {
    not_connected();
};
Exception Thrown when

not_a_dag

topological_sort is called on a graph with a cycle

negative_edge

dijkstra_shortest_paths encounters a negative edge weight

negative_cycle

bellman_ford_shortest_paths detects a negative-weight cycle

not_connected

An algorithm requiring a connected graph receives a disconnected one