artdaq_core  v1_05_07
 All Classes Namespaces Functions
ExceptionHandler.cc
1 
2 // JCF, 5/28/15
3 
4 // The ExceptionHandler() function is designed to be called within a catch-all block:
5 
6 // try {
7 //
8 // ...Code that might throw an exception...
9 //
10 // } catch (...) {
11 //
12 // ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes,
13 // "Optional string providing additional info");
14 //
15 // }
16 
17 // Where above, you could switch out
18 // "artdaq::ExceptionHandlerRethrow::yes" with
19 // "artdaq::ExceptionHandlerRethrow::no", depending on what you wish
20 // to do
21 
22 // The details of ExceptionHandler() are as follows:
23 
24 // -If an optional string is passed to it, use messagefacility to write the string with mf::LogError()
25 
26 // -Apply a set of different catch-blocks to the original exception,
27 // printing out as much information as possible contained within the
28 // different exception types (art::Exception, cet::exception,
29 // boost::exception and std::exception), again using mf::LogError()
30 
31 // -If artdaq::ExceptionHandlerRethrow::yes was passed to
32 // ExceptionHandler(), re-throw the exception rather than swallow it
33 
34 #include "ExceptionHandler.hh"
35 
36 #ifdef HAVE_CANVAS
37 #include "canvas/Utilities/Exception.h"
38 #else
39 #include "art/Utilities/Exception.h"
40 #endif
41 #include "cetlib/exception.h"
42 #include "messagefacility/MessageLogger/MessageLogger.h"
43 
44 #include <boost/exception/all.hpp>
45 
46 #include <stdexcept>
47 
48 namespace artdaq {
49 
50  void ExceptionHandler(ExceptionHandlerRethrow decision, std::string optional_message) {
51 
52  if (optional_message != "") {
53  mf::LogError("ExceptionHandler") << optional_message;
54  }
55 
56  try {
57  throw;
58  } catch (const art::Exception& e) {
59 
60  mf::LogError("ExceptionHandler") << "art::Exception object caught:" <<
61  " returnCode = " << e.returnCode() <<
62  ", categoryCode = " << e.categoryCode() <<
63  ", category = " << e.category();
64  mf::LogError("ExceptionHandler") << "art::Exception object stream:" << e;
65 
66  if (decision == ExceptionHandlerRethrow::yes) { throw; }
67 
68  } catch (const cet::exception &e) {
69 
70  mf::LogError("ExceptionHandler") << "cet::exception object caught:" <<
71  e.explain_self();
72 
73  if (decision == ExceptionHandlerRethrow::yes) { throw; }
74 
75  } catch (const boost::exception& e) {
76 
77  mf::LogError("ExceptionHandler") << "boost::exception object caught: " <<
78  boost::diagnostic_information(e);
79 
80  if (decision == ExceptionHandlerRethrow::yes) { throw; }
81 
82  } catch (const std::exception& e ) {
83 
84  mf::LogError ("ExceptionHandler") << "std::exception caught: " << e.what();
85 
86  if (decision == ExceptionHandlerRethrow::yes) { throw; }
87 
88  } catch (...) {
89 
90  mf::LogError ("ExceptionHandler") << "Exception of type unknown to artdaq::ExceptionHandler caught";
91 
92  if (decision == ExceptionHandlerRethrow::yes) { throw; }
93 
94  }
95  }
96 
97 }