Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef FWCore_Framework_ExceptionHelpers_h
0002 #define FWCore_Framework_ExceptionHelpers_h
0003 
0004 // Original Author:  W. David Dagenhart
0005 //         Created:  March 2012
0006 
0007 // These methods should only be used by the Framework
0008 // internally!
0009 
0010 // These methods are intended to be used at points
0011 // where we catch exceptions earlier than the end
0012 // of the "main" function in cmsRun.cpp. The purpose of
0013 // of catching them early is to print the useful exception
0014 // information before we try cleaning up lumis, runs,
0015 // and files by calling functions like endRun. We have
0016 // experienced problems that seg faults during the
0017 // cleanup caused the primary exception message to be lost.
0018 
0019 // The intent is that printing will be disabled in the
0020 // case where there were additional exceptions after
0021 // a primary exception and these additional exceptions
0022 // where thrown while cleaning up runs, lumis, and files.
0023 // The messages from the exceptions after the first tend
0024 // to confuse people more than help.
0025 
0026 // At the moment, these functions are called after an
0027 // exception occurs in a module's beginRun, beginLumi,
0028 // event, endLumi, or endRun methods. And also if
0029 // the exception occurs in most of the InputSource virtual
0030 // methods.  I expect that usage might be extended to other
0031 // cases. Note these functions are not needed outside of
0032 // the time where the cleanup of runs, lumis and files might
0033 // occur. If the process is before or after that period, it
0034 // is simpler to let the exceptions should just be allowed
0035 // to unwind up the stack into main.
0036 
0037 #include "FWCore/Utilities/interface/ConvertException.h"
0038 #include "FWCore/Utilities/interface/Exception.h"
0039 
0040 #include <exception>
0041 #include <functional>
0042 #include <string>
0043 
0044 namespace edm {
0045 
0046   void addContextAndPrintException(char const* context, cms::Exception& ex, bool disablePrint);
0047 
0048   template <typename TReturn>
0049   TReturn callWithTryCatchAndPrint(std::function<TReturn(void)> iFunc,
0050                                    char const* context = nullptr,
0051                                    bool disablePrint = false) {
0052     try {
0053       return convertException::wrap([iFunc]() { return iFunc(); });
0054     } catch (cms::Exception& ex) {
0055       addContextAndPrintException(context, ex, disablePrint);
0056       throw;
0057     }
0058     return TReturn();
0059   }
0060 }  // namespace edm
0061 
0062 #endif