Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:12

0001 #ifndef FWCore_Utilities_ExceptionCollector_h
0002 #define FWCore_Utilities_ExceptionCollector_h
0003 
0004 /**
0005 ExceptionCollector is a utility class that can be used to make sure that
0006 each function or functor in a sequence of calls is invoked even if 
0007 a previous function throws.  Each function/functor must take no arguments
0008 and return a void.  std::bind can be used to convert a function
0009 taking arguments into a function taking no arguments.
0010 The exception strings are saved in a cms::Exception for optional rethrow.
0011 
0012 Here is an example:
0013 
0014 ExceptionCollector c("initialMessage");
0015 
0016 c.call(std::bind(&MyClass::myFunction, myClassPtr));
0017 c.call(std::bind(&MyClass::myOtherFunction, myClassPtr, myArgPtr));
0018 c.call(std::bind(&myFreeFunction, myArgPtr));
0019 if (c.hasThrown()) c.rethrow();
0020 
0021 This insures that all three functions will be called before any exception is thrown.
0022 **/
0023 
0024 #include <functional>
0025 #include <memory>
0026 #include <string>
0027 
0028 namespace cms {
0029   class Exception;
0030 }
0031 
0032 namespace edm {
0033   class ExceptionCollector {
0034   public:
0035     ExceptionCollector(std::string const& initialMessage);
0036     ~ExceptionCollector();
0037     bool hasThrown() const;
0038     void rethrow() const;
0039     void call(std::function<void(void)>);
0040     void addException(cms::Exception const& exception);
0041 
0042   private:
0043     std::string initialMessage_;
0044     std::unique_ptr<cms::Exception> firstException_;
0045     std::unique_ptr<cms::Exception> accumulatedExceptions_;
0046     int nExceptions_;
0047   };
0048 }  // namespace edm
0049 
0050 #endif