All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Disturbance_module.cc
Go to the documentation of this file.
1 /**
2  * @file larexamples/DebuggingExamples/CatchException/Disturbance_module.cc
3  * @brief A module throwing a standard library exception.
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/EDProducer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "canvas/Utilities/Exception.h"
14 #include "fhiclcpp/types/Atom.h"
15 
16 
17 namespace lar {
18  namespace example {
19 
20  /**
21  * @brief A silly module.
22  *
23  * This module throws and catches a lot of `art::Exception` exceptions.
24  * Kids, don't do this at home!
25  *
26  *
27  * Configuration
28  * ==============
29  *
30  * * *NArtExceptions* (integer, mandatory): number of exceptions to throw
31  *
32  */
33  class Disturbance: public art::EDProducer {
34  public:
35 
36  struct Config {
37 
38  fhicl::Atom<unsigned int> NArtExceptions {
39  fhicl::Name("NArtExceptions"),
40  fhicl::Comment("number of exceptions to throw")
41  };
42 
43  }; // struct Config
44 
45  using Parameters = art::EDProducer::Table<Config>;
46 
47  /// Constructor.
48  Disturbance(Parameters const& config);
49 
50  /// Executes the iterations.
51  virtual void produce(art::Event&) override;
52 
53 
54  private:
55 
56  unsigned int fNArtExceptions;
57 
58  /// Throws a `std::out_of_range` exception.
59  static void throwArtException();
60 
61  }; // class Disturbance
62 
63 
64  } // namespace example
65 } // namespace lar
66 
67 //------------------------------------------------------------------------------
69  : EDProducer{config}
70  , fNArtExceptions(config().NArtExceptions())
71  {}
72 
73 
74 //------------------------------------------------------------------------------
76 
77  //
78  // art::Exception
79  //
80  for (unsigned int i = 0; i < fNArtExceptions; ++i) {
81  try {
82  throwArtException();
83  }
84  catch (art::Exception const&) {}
85  } // for
86 
87 } // lar::example::Disturbance::produce()
88 
89 
90 //------------------------------------------------------------------------------
92 
93  throw art::Exception(art::errors::LogicError)
94  << "I want to annoy you.\n";
95 
96 } // lar::example::Disturbance::throwArtException()
97 
98 
99 //------------------------------------------------------------------------------
100 DEFINE_ART_MODULE(lar::example::Disturbance)
101 
102 
103 //------------------------------------------------------------------------------
art::EDProducer::Table< Config > Parameters
fhicl::Atom< unsigned int > NArtExceptions
virtual void produce(art::Event &) override
Executes the iterations.
static void throwArtException()
Throws a std::out_of_range exception.
BEGIN_PROLOG vertical distance to the surface Name
Disturbance(Parameters const &config)
Constructor.