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 #include "FWCore/Utilities/interface/Exception.h" 00039 00040 #include <exception> 00041 #include <functional> 00042 #include <string> 00043 00044 namespace edm { 00045 00046 void addContextAndPrintException(char const* context, 00047 cms::Exception& ex, 00048 bool disablePrint); 00049 00050 template <typename TReturn> 00051 TReturn callWithTryCatchAndPrint(std::function<TReturn (void)> iFunc, 00052 char const* context = 0, 00053 bool disablePrint = false) { 00054 00055 try { 00056 try { 00057 return iFunc(); 00058 } 00059 catch (cms::Exception& e) { throw; } 00060 catch (std::bad_alloc& bda) { convertException::badAllocToEDM(); } 00061 catch (std::exception& e) { convertException::stdToEDM(e); } 00062 catch (std::string& s) { convertException::stringToEDM(s); } 00063 catch (char const* c) { convertException::charPtrToEDM(c); } 00064 catch (...) { convertException::unknownToEDM(); } 00065 } 00066 catch(cms::Exception& ex) { 00067 addContextAndPrintException(context, ex, disablePrint); 00068 throw; 00069 } 00070 return TReturn(); 00071 } 00072 } 00073 00074 #endif