artdaq_core  v3_06_11
ExceptionHandler.cc
1 
2 #define TRACE_NAME "ExceptionHandler"
3 #include "ExceptionHandler.hh"
4 #include "ExceptionStackTrace.hh"
5 
6 #include "canvas/Utilities/Exception.h"
7 #include "cetlib_except/exception.h"
8 #include "tracemf.h"
9 
10 #include <boost/exception/all.hpp>
11 namespace artdaq {
12 
13 #ifdef EXCEPTIONSTACKTRACE
14 
17 inline void PrintExceptionStackTrace()
18 {
19  auto message = artdaq::debug::getStackTraceCollector().print_stacktrace();
20 
21  std::string::size_type pos = 0;
22  std::string::size_type prev = 0;
23 
24  while ((pos = message.find('\n', prev)) != std::string::npos)
25  {
26  TLOG(TLVL_DEBUG) << message.substr(prev, pos - prev);
27  prev = pos + 1;
28  }
29 
30  TLOG(TLVL_DEBUG) << message.substr(prev);
31 }
32 #else
33 
37 {}
38 #endif
39 
40 void ExceptionHandler(ExceptionHandlerRethrow decision, const std::string& optional_message)
41 {
42  if (!optional_message.empty())
43  {
44  TLOG(TLVL_ERROR) << optional_message;
45  }
46 
47  try
48  {
49  throw;
50  }
51  catch (const art::Exception& e)
52  {
53  TLOG(TLVL_ERROR) << "art::Exception object caught:"
54  << " returnCode = " << e.returnCode() << ", categoryCode = " << e.categoryCode() << ", category = " << e.category();
55  TLOG(TLVL_ERROR) << "art::Exception object stream:" << e;
57 
58  if (decision == ExceptionHandlerRethrow::yes) { throw; }
59  }
60  catch (const cet::exception& e)
61  {
62  TLOG(TLVL_ERROR) << "cet::exception object caught:" << e.explain_self();
64 
65  if (decision == ExceptionHandlerRethrow::yes) { throw; }
66  }
67  catch (const boost::exception& e)
68  {
69  TLOG(TLVL_ERROR) << "boost::exception object caught: " << boost::diagnostic_information(e);
71 
72  if (decision == ExceptionHandlerRethrow::yes) { throw; }
73  }
74  catch (const std::exception& e)
75  {
76  TLOG(TLVL_ERROR) << "std::exception caught: " << e.what();
78 
79  if (decision == ExceptionHandlerRethrow::yes) { throw; }
80  }
81  catch (...)
82  {
83  TLOG(TLVL_ERROR) << "Exception of type unknown to artdaq::ExceptionHandler caught";
85 
86  if (decision == ExceptionHandlerRethrow::yes) { throw; }
87  }
88 }
89 } // namespace artdaq
ExceptionHandlerRethrow
Controls whether the ExceptionHandler will rethrow after printing exception details.
void PrintExceptionStackTrace()
Print the Exception Stack Trace.
Rethrow the exception after sending details to MessageFacility.
void ExceptionHandler(ExceptionHandlerRethrow decision, const std::string &optional_message)
The ExceptionHandler class prints out all available information about an excection, then optionally re-throws.
std::string print_stacktrace()
Produces a stack trace summary.