Expose exception of restartControlLoops().

Expose the failure of restartControlLoops() for the last loop. The
orignal rethrowing an exception will lose the call stack infomation.
It leads to extreme difficulties when debugging.

Comparing to embedding the traceback infomation into the new (nested)
exception, I would rather expose the exception directly to external
which preserves both call stack and varibale information.

Signed-off-by: Hao Jiang <jianghao@google.com>
Change-Id: I1535fc5768ea58e5af695948eec65fdfa84e8ff9
diff --git a/main.cpp b/main.cpp
index 1cd6244..c49c522 100644
--- a/main.cpp
+++ b/main.cpp
@@ -170,34 +170,36 @@
         timer.expires_after(delayTeime);
     }
     count++;
-    timer.async_wait(
-        [](const boost::system::error_code& error) {
-            if (error == boost::asio::error::operation_aborted)
-            {
-                return;
-            }
-            try
-            {
-                restartControlLoops();
-                // reset count after succesful restartControlLoops()
-                count = 0;
-            }
-            catch (const std::exception& e)
-            {
-                if (count >= 5)
-                {
-                    std::cerr
-                        << "Failed to restartControlLoops after multiple tries"
-                        << std::endl;
-                    throw std::runtime_error(e.what());
-                }
+    timer.async_wait([](const boost::system::error_code& error) {
+        if (error == boost::asio::error::operation_aborted)
+        {
+            return;
+        }
 
-                std::cerr << count
-                          << " Failed during restartControlLoops, try again: "
-                          << e.what() << "\n";
-                tryRestartControlLoops(false);
-            }
-        });
+        // for the last loop, don't elminate the failure of restartControlLoops.
+        if (count >= 5)
+        {
+            restartControlLoops();
+            // reset count after succesful restartControlLoops()
+            count = 0;
+            return;
+        }
+
+        // retry when restartControlLoops() has some failure.
+        try
+        {
+            restartControlLoops();
+            // reset count after succesful restartControlLoops()
+            count = 0;
+        }
+        catch (const std::exception& e)
+        {
+            std::cerr << count
+                      << " Failed during restartControlLoops, try again: "
+                      << e.what() << "\n";
+            tryRestartControlLoops(false);
+        }
+    });
 
     return;
 }