artdaq_core  v1_06_00
 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 #include "canvas/Utilities/Exception.h"
37 #include "cetlib/exception.h"
38 #include "messagefacility/MessageLogger/MessageLogger.h"
39 
40 #include <boost/exception/all.hpp>
41 
42 #include <stdexcept>
43 
44 namespace artdaq {
45 
46  void ExceptionHandler(ExceptionHandlerRethrow decision, std::string optional_message) {
47 
48  if (optional_message != "") {
49  mf::LogError("ExceptionHandler") << optional_message;
50  }
51 
52  try {
53  throw;
54  } catch (const art::Exception& e) {
55 
56  mf::LogError("ExceptionHandler") << "art::Exception object caught:" <<
57  " returnCode = " << e.returnCode() <<
58  ", categoryCode = " << e.categoryCode() <<
59  ", category = " << e.category();
60  mf::LogError("ExceptionHandler") << "art::Exception object stream:" << e;
61 
62  if (decision == ExceptionHandlerRethrow::yes) { throw; }
63 
64  } catch (const cet::exception &e) {
65 
66  mf::LogError("ExceptionHandler") << "cet::exception object caught:" <<
67  e.explain_self();
68 
69  if (decision == ExceptionHandlerRethrow::yes) { throw; }
70 
71  } catch (const boost::exception& e) {
72 
73  mf::LogError("ExceptionHandler") << "boost::exception object caught: " <<
74  boost::diagnostic_information(e);
75 
76  if (decision == ExceptionHandlerRethrow::yes) { throw; }
77 
78  } catch (const std::exception& e ) {
79 
80  mf::LogError ("ExceptionHandler") << "std::exception caught: " << e.what();
81 
82  if (decision == ExceptionHandlerRethrow::yes) { throw; }
83 
84  } catch (...) {
85 
86  mf::LogError ("ExceptionHandler") << "Exception of type unknown to artdaq::ExceptionHandler caught";
87 
88  if (decision == ExceptionHandlerRethrow::yes) { throw; }
89 
90  }
91  }
92 
93 }