artdaq_core  v3_06_10
ExceptionHandler_t.cc
1 #include "artdaq-core/Utilities/ExceptionHandler.hh"
2 
3 #define BOOST_TEST_MODULE ExceptionHandler_t
4 #include "cetlib/quiet_unit_test.hpp"
5 
6 #define TRACE_NAME "ExceptionHandler_t"
7 #include "canvas/Utilities/Exception.h"
8 #include "cetlib_except/exception.h"
9 #include "tracemf.h"
10 
11 #include <boost/exception/all.hpp>
12 
13 BOOST_AUTO_TEST_SUITE(ExceptionHandler_test)
14 
15 typedef boost::error_info<struct tag_my_info, std::string> my_info;
16 
17 struct my_error : virtual boost::exception, virtual std::exception
18 {};
19 
20 BOOST_AUTO_TEST_CASE(artException)
21 {
22  try
23  {
24  throw art::Exception(art::errors::Unknown, "TestException");
25  }
26  catch (art::Exception& e)
27  {
28  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "This is a test of art::Exception");
29  BOOST_REQUIRE_EXCEPTION(
30  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes, "This is another test of art::Exception"), art::Exception, [](art::Exception const& e) { return e.category() == e.codeToString(art::errors::Unknown); });
31  }
32 }
33 BOOST_AUTO_TEST_CASE(cetException)
34 {
35  try
36  {
37  throw cet::exception("TestException");
38  }
39  catch (cet::exception&)
40  {
41  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "This is a test of cet::exception");
42  BOOST_REQUIRE_EXCEPTION(
43  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes, "This is another test of cet::exception"), cet::exception, [](cet::exception const& e) { return e.category() == "TestException"; });
44  }
45 }
46 BOOST_AUTO_TEST_CASE(boostException)
47 {
48  try
49  {
50  throw my_error() << my_info("TestException");
51  }
52  catch (boost::exception&)
53  {
54  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "This is a test of boost::exception");
55  BOOST_REQUIRE_EXCEPTION(
56  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes, "This is another test of boost::exception"), boost::exception, [](boost::exception const&) { return true; });
57  }
58 }
59 BOOST_AUTO_TEST_CASE(stdException)
60 {
61  try
62  {
63  throw std::exception();
64  }
65  catch (std::exception&)
66  {
67  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "This is a test of std::exception");
68  BOOST_REQUIRE_EXCEPTION(
69  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes, "This is another test of std::exception"), std::exception, [](std::exception const&) { return true; });
70  }
71 }
72 BOOST_AUTO_TEST_CASE(arbitraryThrow)
73 {
74  try
75  {
76  throw int(5);
77  }
78  catch (...)
79  {
80  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::no, "This is a test of arbitrary throw handling");
81  BOOST_REQUIRE_EXCEPTION(
82  artdaq::ExceptionHandler(artdaq::ExceptionHandlerRethrow::yes, "This is another test of arbitrary throw handling"), int, [](int const& e) { return e == 5; });
83  }
84 }
85 
86 BOOST_AUTO_TEST_SUITE_END()
Consume the exception and proceed.
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.