CMS 3D CMS Logo

ExceptionHelpers.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_ExceptionHelpers_h
2 #define FWCore_Framework_ExceptionHelpers_h
3 
4 // Original Author: W. David Dagenhart
5 // Created: March 2012
6 
7 // These methods should only be used by the Framework
8 // internally!
9 
10 // These methods are intended to be used at points
11 // where we catch exceptions earlier than the end
12 // of the "main" function in cmsRun.cpp. The purpose of
13 // of catching them early is to print the useful exception
14 // information before we try cleaning up lumis, runs,
15 // and files by calling functions like endRun. We have
16 // experienced problems that seg faults during the
17 // cleanup caused the primary exception message to be lost.
18 
19 // The intent is that printing will be disabled in the
20 // case where there were additional exceptions after
21 // a primary exception and these additional exceptions
22 // where thrown while cleaning up runs, lumis, and files.
23 // The messages from the exceptions after the first tend
24 // to confuse people more than help.
25 
26 // At the moment, these functions are called after an
27 // exception occurs in a module's beginRun, beginLumi,
28 // event, endLumi, or endRun methods. And also if
29 // the exception occurs in most of the InputSource virtual
30 // methods. I expect that usage might be extended to other
31 // cases. Note these functions are not needed outside of
32 // the time where the cleanup of runs, lumis and files might
33 // occur. If the process is before or after that period, it
34 // is simpler to let the exceptions should just be allowed
35 // to unwind up the stack into main.
36 
39 
40 #include <exception>
41 #include <functional>
42 #include <string>
43 
44 namespace edm {
45 
46  void addContextAndPrintException(char const* context,
47  cms::Exception& ex,
48  bool disablePrint);
49 
50  template <typename TReturn>
51  TReturn callWithTryCatchAndPrint(std::function<TReturn (void)> iFunc,
52  char const* context = nullptr,
53  bool disablePrint = false) {
54 
55  try {
56  return convertException::wrap([iFunc]() {
57  return iFunc();
58  });
59  }
60  catch(cms::Exception& ex) {
61  addContextAndPrintException(context, ex, disablePrint);
62  throw;
63  }
64  return TReturn();
65  }
66 }
67 
68 #endif
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
TReturn callWithTryCatchAndPrint(std::function< TReturn(void)> iFunc, char const *context=0, bool disablePrint=false)
HLT enums.
auto wrap(F iFunc) -> decltype(iFunc())