Thread-safe version of loopKeepAlive()
[folly.git] / folly / ExceptionWrapper.h
index 7909fb053c56aa4b5678e26767cc2242ddf33e89..df40f040367efb85e37dcd56cae294c45d564a85 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#ifndef FOLLY_EXCEPTIONWRAPPER_H
-#define FOLLY_EXCEPTIONWRAPPER_H
+#pragma once
 
 #include <cassert>
 #include <exception>
+#include <iostream>
 #include <memory>
-#include <folly/String.h>
+#include <folly/ExceptionString.h>
 #include <folly/detail/ExceptionWrapper.h>
 
 namespace folly {
@@ -149,12 +149,18 @@ class exception_wrapper {
     assign_eptr(eptr);
   }
 
-  void throwException() const {
+  // If the exception_wrapper does not contain an exception, std::terminate()
+  // is invoked to assure the [[noreturn]] behaviour.
+  [[noreturn]] void throwException() const {
     if (throwfn_) {
       throwfn_(item_.get());
     } else if (eptr_) {
       std::rethrow_exception(eptr_);
     }
+    std::cerr
+        << "Cannot use `throwException` with an empty folly::exception_wrapper"
+        << std::endl;
+    std::terminate();
   }
 
   explicit operator bool() const {
@@ -261,7 +267,9 @@ class exception_wrapper {
     bool>::type
   with_exception(F f) const {
     try {
-      throwException();
+      if (*this) {
+        throwException();
+      }
     } catch (typename std::decay<Ex>::type& e) {
       f(e);
       return true;
@@ -277,7 +285,9 @@ class exception_wrapper {
     }
 
     try {
-      throwException();
+      if (*this) {
+        throwException();
+      }
     } catch (...) {
       return std::current_exception();
     }
@@ -468,4 +478,3 @@ class try_and_catch<> : public exception_wrapper {
   }
 };
 }
-#endif