artdaq_core  v3_06_01
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 inline void PrintExceptionStackTrace()
15 {
16  auto message = artdaq::debug::getStackTraceCollector().print_stacktrace();
17 
18  std::string::size_type pos = 0;
19  std::string::size_type prev = 0;
20 
21  while ((pos = message.find('\n', prev)) != std::string::npos)
22  {
23  TLOG(TLVL_DEBUG) << message.substr(prev, pos - prev);
24  prev = pos + 1;
25  }
26 
27  TLOG(TLVL_DEBUG) << message.substr(prev);
28 }
29 #else
30 inline void PrintExceptionStackTrace()
31 {}
32 #endif
33 
34 void ExceptionHandler(ExceptionHandlerRethrow decision, const std::string& optional_message)
35 {
36  if (!optional_message.empty())
37  {
38  TLOG(TLVL_ERROR) << optional_message;
39  }
40 
41  try
42  {
43  throw;
44  }
45  catch (const art::Exception& e)
46  {
47  TLOG(TLVL_ERROR) << "art::Exception object caught:"
48  << " returnCode = " << e.returnCode() << ", categoryCode = " << e.categoryCode() << ", category = " << e.category();
49  TLOG(TLVL_ERROR) << "art::Exception object stream:" << e;
50  PrintExceptionStackTrace();
51 
52  if (decision == ExceptionHandlerRethrow::yes) { throw; }
53  }
54  catch (const cet::exception& e)
55  {
56  TLOG(TLVL_ERROR) << "cet::exception object caught:" << e.explain_self();
57  PrintExceptionStackTrace();
58 
59  if (decision == ExceptionHandlerRethrow::yes) { throw; }
60  }
61  catch (const boost::exception& e)
62  {
63  TLOG(TLVL_ERROR) << "boost::exception object caught: " << boost::diagnostic_information(e);
64  PrintExceptionStackTrace();
65 
66  if (decision == ExceptionHandlerRethrow::yes) { throw; }
67  }
68  catch (const std::exception& e)
69  {
70  TLOG(TLVL_ERROR) << "std::exception caught: " << e.what();
71  PrintExceptionStackTrace();
72 
73  if (decision == ExceptionHandlerRethrow::yes) { throw; }
74  }
75  catch (...)
76  {
77  TLOG(TLVL_ERROR) << "Exception of type unknown to artdaq::ExceptionHandler caught";
78  PrintExceptionStackTrace();
79 
80  if (decision == ExceptionHandlerRethrow::yes) { throw; }
81  }
82 }
83 } // namespace artdaq
ExceptionHandlerRethrow
Controls whether the ExceptionHandler will rethrow after printing exception details.
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.