1.. _catching_exceptions: 2 3Catching Exceptions Inside the Node that Throws the Exception 4============================================================= 5 6 7If you catch an exception within the node's body, execution continues 8normally, as you might expect. If an exception is thrown but is not 9caught before it propagates beyond the node's body, the execution of all 10of the graph's nodes are canceled and the exception is rethrown at the 11call site of graph::wait_for_all(). Take the graph below as an example: 12 13 14:: 15 16 17 graph g; 18 19 20 function_node< int, int > f1( g, 1, []( int i ) { return i; } ); 21 22 23 function_node< int, int > f2( g, 1, 24 []( const int i ) -> int { 25 throw i; 26 return i; 27 } ); 28 29 30 function_node< int, int > f3( g, 1, []( int i ) { return i; } ); 31 32 33 make_edge( f1, f2 ); 34 make_edge( f2, f3 ); 35 f1.try_put(1); 36 f1.try_put(2); 37 g.wait_for_all(); 38 39 40In the code above, the second function_node, f2, throws an exception 41that is not caught within the body. This will cause the execution of the 42graph to be canceled and the exception to be rethrown at the call to 43g.wait_for_all(). Since it is not handled there either, the program will 44terminate. If desirable, the exception could be caught and handled 45within the body: 46 47 48:: 49 50 51 function_node< int, int > f2( g, 1, 52 []( const int i ) -> int { 53 try { 54 throw i; 55 } catch (int j) { 56 cout << "Caught " << j << "\n"; 57 } 58 return i; 59 } ); 60 61 62If the exception is caught and handled in the body, then there is no 63effect on the overall execution of the graph. However, you could choose 64instead to catch the exception at the call to wait_for_all: 65 66 67:: 68 69 70 try { 71 g.wait_for_all(); 72 } catch ( int j ) { 73 cout << "Caught " << j << "\n"; 74 } 75 76 77In this case, the execution of the graph is canceled. For our example, 78this means that the input 1 never reaches f3 and that input 2 never 79reaches either f2 or f3. 80 81