All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Exploder_module.cc
Go to the documentation of this file.
1 /**
2  * @file larexamples/DebuggingExamples/CatchException/Exploder_module.cc
3  * @brief A module throwing exceptions.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date March 21, 2018
6  * @ingroup CatchException
7  */
8 
9 
10 // framework libraries
11 #include "art/Framework/Core/EDAnalyzer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "canvas/Utilities/Exception.h"
14 #include "messagefacility/MessageLogger/MessageLogger.h"
15 #include "fhiclcpp/types/Atom.h"
16 
17 // C/C++ standard libraries
18 #include <array>
19 #include <stdexcept> // std::out_of_range, std::length_error
20 #include <new> // std::bad_alloc
21 
22 
23 namespace lar {
24  namespace example {
25 
26  /**
27  * @brief A buggy module.
28  *
29  * This module executes a number of iterations.
30  * It does something in some of those iterations, depending on the module
31  * configuration.
32  *
33  *
34  * Configuration
35  * ==============
36  *
37  * * *NIterations* (integer, default: `10`): number of iterations executed
38  * * *OutOfRange* (integer, optional): if specified, the module throws a
39  * `std::out_of_range` exception at the specified loop number
40  *
41  */
42  class Exploder: public art::EDAnalyzer {
43  public:
44 
45  struct Config {
46 
47  fhicl::Atom<bool> ManageBadAlloc {
48  fhicl::Name("ManageBadAlloc"),
49  fhicl::Comment("whether to catch the std::bad_alloc we throw"),
50  true // default
51  };
52 
53  fhicl::Atom<bool> ManageOutOfRange {
54  fhicl::Name("ManageOutOfRange"),
55  fhicl::Comment("whether to catch the std::out_of_range we throw"),
56  true // default
57  };
58 
59  fhicl::Atom<bool> ManageArtException {
60  fhicl::Name("ManageArtException"),
61  fhicl::Comment("whether to catch the art::Exception we throw"),
62  true // default
63  };
64 
65  }; // struct Config
66 
67  using Parameters = art::EDAnalyzer::Table<Config>;
68 
69  /// Constructor.
70  Exploder(Parameters const& config);
71 
72  /// Executes the iterations.
73  virtual void analyze(art::Event const&) override;
74 
75 
76  private:
77 
81 
82  /// Throws a `std::length_error` exception.
83  static unsigned int throwOutOfRange();
84 
85  /// Throws a `std::out_of_range` exception.
86  static void throwBadAlloc();
87 
88  /// Throws a `std::out_of_range` exception.
89  static void throwArtException();
90 
91  }; // class Exploder
92 
93 
94  } // namespace example
95 } // namespace lar
96 
97 //------------------------------------------------------------------------------
99  : art::EDAnalyzer(config)
100  , fManageBadAlloc(config().ManageBadAlloc())
101  , fManageOutOfRange(config().ManageOutOfRange())
102  , fManageArtException(config().ManageArtException())
103  {}
104 
105 
106 //------------------------------------------------------------------------------
107 void lar::example::Exploder::analyze(art::Event const&) {
108 
109  //
110  // std::length_error
111  //
112  if (fManageBadAlloc) {
113  try {
114  throwBadAlloc();
115  }
116  catch (std::bad_alloc const&) {}
117  }
118  else {
119  throwBadAlloc();
120  }
121 
122  //
123  // std::out_of_range
124  //
125  if (fManageOutOfRange) {
126  try {
127  throwOutOfRange();
128  }
129  catch (std::out_of_range const&) {}
130  }
131  else {
132  throwOutOfRange();
133  }
134 
135  //
136  // art::Exception
137  //
138  if (fManageArtException) {
139  try {
140  throwArtException();
141  }
142  catch (art::Exception const&) {}
143  }
144  else {
145  throwArtException();
146  }
147 
148 } // lar::example::Exploder::analyze()
149 
150 
151 //------------------------------------------------------------------------------
153 
154  std::vector<int> intData(5, 0);
155 
156  int intTotal = 0;
157  for (unsigned int i = 0; i < 10; ++i) {
158  mf::LogVerbatim("Exploder") << "Starting TOOR iteration #" << i;
159 
160  // possible std::out_of_range throw
161  intTotal += intData.at(i);
162 
163  } // for
164  mf::LogVerbatim("Exploder") << "TOOR iterations completed.";
165 
166  return intTotal;
167 } // lar::example::Exploder::throwOutOfRange()
168 
169 
170 //------------------------------------------------------------------------------
172 
173  using OneMebibyte = std::array<unsigned char, 1048576U>;
174 
175  std::vector<OneMebibyte> manyMebibytes;
176 
177  // this is allowed, but we don't have enough memory
178  mf::LogVerbatim("Exploder") << "Now allocating: " << manyMebibytes.max_size()
179  << " x " << sizeof(OneMebibyte) << " bytes";
180  manyMebibytes.resize(manyMebibytes.max_size());
181 
182 } // lar::example::Exploder::throwBadAlloc()
183 
184 
185 //------------------------------------------------------------------------------
187 
188  throw art::Exception(art::errors::LogicError)
189  << "I hate the world and I am vengeful.\n";
190 
191 } // lar::example::Exploder::throwArtException()
192 
193 
194 //------------------------------------------------------------------------------
195 DEFINE_ART_MODULE(lar::example::Exploder)
196 
197 
198 //------------------------------------------------------------------------------
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.
BEGIN_PROLOG vertical distance to the surface Name
static void throwBadAlloc()
Throws a std::out_of_range exception.
art::EDAnalyzer::Table< Config > Parameters
fhicl::Atom< bool > ManageOutOfRange
static void throwArtException()
Throws a std::out_of_range exception.