CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
38 
39 #include <exception>
40 #include <functional>
41 #include <string>
42 
43 namespace cms {
44  class Exception;
45 }
46 
47 namespace edm {
48 
49  void addContextAndPrintException(char const* context,
50  cms::Exception& ex,
51  bool disablePrint);
52 
53  template <typename TReturn>
54  TReturn callWithTryCatchAndPrint(std::function<TReturn (void)> iFunc,
55  char const* context = 0,
56  bool disablePrint = false) {
57 
58  try {
59  try {
60  return iFunc();
61  }
62  catch (cms::Exception& e) { throw; }
63  catch (std::bad_alloc& bda) { convertException::badAllocToEDM(); }
65  catch (std::string& s) { convertException::stringToEDM(s); }
66  catch (char const* c) { convertException::charPtrToEDM(c); }
67  catch (...) { convertException::unknownToEDM(); }
68  }
69  catch(cms::Exception& ex) {
70  addContextAndPrintException(context, ex, disablePrint);
71  throw;
72  }
73  return TReturn();
74  }
75 }
76 
77 #endif
void addContextAndPrintException(char const *context, cms::Exception &ex, bool disablePrint)
void stdToEDM(std::exception const &e)
helper::RootFunctionHelper< F, args >::root_function function(F &f)
Definition: rootFunction.h:14
TReturn callWithTryCatchAndPrint(std::function< TReturn(void)> iFunc, char const *context=0, bool disablePrint=false)
void charPtrToEDM(char const *c)
void stringToEDM(std::string &s)