00001 #ifndef FWCore_Framework_ExceptionHelpers_h 00002 #define FWCore_Framework_ExceptionHelpers_h 00003 00004 // Original Author: W. David Dagenhart 00005 // Created: March 2012 00006 00007 // These methods should only be used by the Framework 00008 // internally! 00009 00010 // These methods are intended to be used at points 00011 // where we catch exceptions earlier than the end 00012 // of the "main" function in cmsRun.cpp. The purpose of 00013 // of catching them early is to print the useful exception 00014 // information before we try cleaning up lumis, runs, 00015 // and files by calling functions like endRun. We have 00016 // experienced problems that seg faults during the 00017 // cleanup caused the primary exception message to be lost. 00018 00019 // The intent is that printing will be disabled in the 00020 // case where there were additional exceptions after 00021 // a primary exception and these additional exceptions 00022 // where thrown while cleaning up runs, lumis, and files. 00023 // The messages from the exceptions after the first tend 00024 // to confuse people more than help. 00025 00026 // At the moment, these functions are called after an 00027 // exception occurs in a module's beginRun, beginLumi, 00028 // event, endLumi, or endRun methods. And also if 00029 // the exception occurs in most of the InputSource virtual 00030 // methods. I expect that usage might be extended to other 00031 // cases. Note these functions are not needed outside of 00032 // the time where the cleanup of runs, lumis and files might 00033 // occur. If the process is before or after that period, it 00034 // is simpler to let the exceptions should just be allowed 00035 // to unwind up the stack into main. 00036 00037 #include "FWCore/Utilities/interface/ConvertException.h" 00038 00039 #include <exception> 00040 #include <functional> 00041 #include <string> 00042 00043 namespace cms { 00044 class Exception; 00045 } 00046 00047 namespace edm { 00048 00049 void addContextAndPrintException(char const* context, 00050 cms::Exception& ex, 00051 bool disablePrint); 00052 00053 template <typename TReturn> 00054 TReturn callWithTryCatchAndPrint(std::function<TReturn (void)> iFunc, 00055 char const* context = 0, 00056 bool disablePrint = false) { 00057 00058 try { 00059 try { 00060 return iFunc(); 00061 } 00062 catch (cms::Exception& e) { throw; } 00063 catch (std::bad_alloc& bda) { convertException::badAllocToEDM(); } 00064 catch (std::exception& e) { convertException::stdToEDM(e); } 00065 catch (std::string& s) { convertException::stringToEDM(s); } 00066 catch (char const* c) { convertException::charPtrToEDM(c); } 00067 catch (...) { convertException::unknownToEDM(); } 00068 } 00069 catch(cms::Exception& ex) { 00070 addContextAndPrintException(context, ex, disablePrint); 00071 throw; 00072 } 00073 return TReturn(); 00074 } 00075 } 00076 00077 #endif