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
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 
48  fhicl::Name("ManageBadAlloc"),
49  fhicl::Comment("whether to catch the std::bad_alloc we throw"),
50  true // default
51  };
52 
54  fhicl::Name("ManageOutOfRange"),
55  fhicl::Comment("whether to catch the std::out_of_range we throw"),
56  true // default
57  };
58 
60  fhicl::Name("ManageArtException"),
61  fhicl::Comment("whether to catch the art::Exception we throw"),
62  true // default
63  };
64 
65  }; // struct Config
66 
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())
103  {}
104 
105 
106 //------------------------------------------------------------------------------
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 {
141  }
142  catch (art::Exception const&) {}
143  }
144  else {
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 
189  << "I hate the world and I am vengeful.\n";
190 
191 } // lar::example::Exploder::throwArtException()
192 
193 
194 //------------------------------------------------------------------------------
196 
197 
198 //------------------------------------------------------------------------------
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.
ChannelGroupService::Name Name
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
static Config * config
Definition: config.cpp:1054
static void throwBadAlloc()
Throws a std::out_of_range exception.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define Comment
LArSoft-specific namespace.
fhicl::Atom< bool > ManageOutOfRange
static void throwArtException()
Throws a std::out_of_range exception.