LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Exploder_module.cc
Go to the documentation of this file.
1 
9 // framework libraries
13 #include "fhiclcpp/types/Atom.h"
15 
16 // C/C++ standard libraries
17 #include <array>
18 #include <new> // std::bad_alloc
19 #include <stdexcept> // std::out_of_range, std::length_error
20 
21 namespace lar {
22  namespace example {
23 
40  class Exploder : public art::EDAnalyzer {
41  public:
42  struct Config {
43 
45  fhicl::Name("ManageBadAlloc"),
46  fhicl::Comment("whether to catch the std::bad_alloc we throw"),
47  true // default
48  };
49 
51  fhicl::Name("ManageOutOfRange"),
52  fhicl::Comment("whether to catch the std::out_of_range we throw"),
53  true // default
54  };
55 
57  fhicl::Name("ManageArtException"),
58  fhicl::Comment("whether to catch the art::Exception we throw"),
59  true // default
60  };
61 
62  }; // struct Config
63 
65 
67  Exploder(Parameters const& config);
68 
70  virtual void analyze(art::Event const&) override;
71 
72  private:
76 
78  static unsigned int throwOutOfRange();
79 
81  static void throwBadAlloc();
82 
84  static void throwArtException();
85 
86  }; // class Exploder
87 
88  } // namespace example
89 } // namespace lar
90 
91 //------------------------------------------------------------------------------
93  : art::EDAnalyzer(config)
94  , fManageBadAlloc(config().ManageBadAlloc())
97 {}
98 
99 //------------------------------------------------------------------------------
101 {
102 
103  //
104  // std::length_error
105  //
106  if (fManageBadAlloc) {
107  try {
108  throwBadAlloc();
109  }
110  catch (std::bad_alloc const&) {
111  }
112  }
113  else {
114  throwBadAlloc();
115  }
116 
117  //
118  // std::out_of_range
119  //
120  if (fManageOutOfRange) {
121  try {
122  throwOutOfRange();
123  }
124  catch (std::out_of_range const&) {
125  }
126  }
127  else {
128  throwOutOfRange();
129  }
130 
131  //
132  // art::Exception
133  //
134  if (fManageArtException) {
135  try {
137  }
138  catch (art::Exception const&) {
139  }
140  }
141  else {
143  }
144 
145 } // lar::example::Exploder::analyze()
146 
147 //------------------------------------------------------------------------------
149 {
150 
151  std::vector<int> intData(5, 0);
152 
153  int intTotal = 0;
154  for (unsigned int i = 0; i < 10; ++i) {
155  mf::LogVerbatim("Exploder") << "Starting TOOR iteration #" << i;
156 
157  // possible std::out_of_range throw
158  intTotal += intData.at(i);
159 
160  } // for
161  mf::LogVerbatim("Exploder") << "TOOR iterations completed.";
162 
163  return intTotal;
164 } // lar::example::Exploder::throwOutOfRange()
165 
166 //------------------------------------------------------------------------------
168 {
169 
170  using OneMebibyte = std::array<unsigned char, 1048576U>;
171 
172  std::vector<OneMebibyte> manyMebibytes;
173 
174  // this is allowed, but we don't have enough memory
175  mf::LogVerbatim("Exploder") << "Now allocating: " << manyMebibytes.max_size() << " x "
176  << sizeof(OneMebibyte) << " bytes";
177  manyMebibytes.resize(manyMebibytes.max_size());
178 
179 } // lar::example::Exploder::throwBadAlloc()
180 
181 //------------------------------------------------------------------------------
183 {
184 
185  throw art::Exception(art::errors::LogicError) << "I hate the world and I am vengeful.\n";
186 
187 } // lar::example::Exploder::throwArtException()
188 
189 //------------------------------------------------------------------------------
191 
192 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
Exploder(Parameters const &config)
Constructor.
static unsigned int throwOutOfRange()
Throws a std::length_error exception.
fhicl::Atom< bool > ManageArtException
fhicl::Atom< bool > ManageBadAlloc
virtual void analyze(art::Event const &) override
Executes the iterations.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
static void throwBadAlloc()
Throws a std::out_of_range exception.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
LArSoft-specific namespace.
fhicl::Atom< bool > ManageOutOfRange
Definition: MVAAlg.h:12
static void throwArtException()
Throws a std::out_of_range exception.