Exception_t.cc
Go to the documentation of this file.
1 
2 #include "cetlib_except/exception.h"
3 
4 #include <cassert>
5 #include <iomanip>
6 #include <iostream>
7 #include <string>
8 
9 struct Thing {
10  Thing() : x() {}
11  explicit Thing(int xx) : x(xx) {}
12  int x;
13 };
14 
15 std::ostream&
16 operator<<(std::ostream& os, const Thing& t)
17 {
18  os << "Thing(" << t.x << ")";
19  return os;
20 }
21 
22 const char expected[] = "---- InfiniteLoop BEGIN\n"
23  " In func1\n"
24  " ---- DataCorrupt BEGIN\n"
25  " This is just a test: \n"
26  " double: 1.11111\n"
27  " float: 2.22222\n"
28  " uint: 75\n"
29  " string: a string\n"
30  " char*: a nonconst pointer\n"
31  " char[]: a c-style array\n"
32  " Thing: Thing(4)\n"
33  " \n"
34  " double: 1.111110e+00\n"
35  " float: 2.22e+00\n"
36  " char*: ..a nonconst pointer\n"
37  " \n"
38  " ---- DataCorrupt END\n"
39  " Gave up\n"
40  "---- InfiniteLoop END\n";
41 
42 void
44 {
45  double d = 1.11111;
46  float f = 2.22222;
47  unsigned int i = 75U;
48  std::string s("a string");
49  char* c1 = const_cast<char*>("a nonconst pointer");
50  char c2[] = "a c-style array";
51  Thing thing(4);
52 
53  // throw cms::Exception("DataCorrupt")
54  cet::exception e("DataCorrupt");
55  e << "This is just a test: \n"
56  << "double: " << d << "\n"
57  << "float: " << f << "\n"
58  << "uint: " << i << "\n"
59  << "string: " << s << "\n"
60  << "char*: " << c1 << "\n"
61  << "char[]: " << c2 << "\n"
62  << "Thing: " << thing << "\n"
63  << std::endl
64  << "double: " << std::scientific << d << "\n"
65  << "float: " << std::setprecision(2) << f << "\n"
66  << "char*: " << std::setfill('.') << std::setw(20) << c1 << "\n"
67  << std::endl;
68 
69  throw e;
70 }
71 
72 void
74 {
75  func3();
76 }
77 
78 void
80 {
81  try {
82  func2();
83  }
84  catch (cet::exception& e) {
85  throw cet::exception("InfiniteLoop", "In func1", e) << "Gave up";
86  }
87 }
88 
89 // const char answer[] =
90 // "---- InfiniteLoop BEGIN\n"
91 // "In func2\n"
92 // "---- DataCorrupt BEGIN\n"
93 // "This is just a test: \n"
94 // "double: 1.11111\n"
95 // "float: 2.22222\n"
96 // "uint: 4294967295\n"
97 // "string: a string\n"
98 // "char*: a nonconst pointer\n"
99 // "char[]: a c-style array\n"
100 // "Thing: Thing(4)\n"
101 // "\n"
102 // "double: 1.111110e+00\n"
103 // "float: 2.22e+00\n"
104 // "uint: 4294967295\n"
105 // "string: a string\n"
106 // "char*: ..a nonconst pointer\n"
107 // "char[]: a c-style array\n"
108 // "Thing: Thing(4)\n"
109 // "---- DataCorrupt END\n"
110 // "Gave up\n"
111 // "---- InfiniteLoop END\n"
112 
113 const char* correct[] = {"InfiniteLoop", "DataCorrupt"};
114 
115 int
117 {
118  try {
119  func1();
120  }
121  catch (cet::exception& e) {
122  std::cerr << "*** main caught Exception, output is ***\n"
123  << "(" << e.explain_self() << ")\n"
124  << "*** After exception output ***" << std::endl;
125 
126  if (e.explain_self() != expected) {
127  std::cerr << "not right answer\n(" << expected << ")\n" << std::endl;
128  abort();
129  }
130 
131  std::cerr << "\nCategory name list:\n";
132 
133  cet::exception::CategoryList::const_iterator i(e.history().begin()),
134  b(e.history().end());
135 
136  // if(e.history().size() !=2) abort();
137  assert(e.history().size() == 2);
138 
139  for (int j = 0; i != b; ++i, ++j) {
140  std::cout << " " << *i << "\n";
141  if (*i != correct[j]) {
142  std::cerr << "bad category " << *i << std::endl;
143  abort();
144  }
145  }
146  }
147  return 0;
148 }
std::ostream & operator<<(std::ostream &os, const Thing &t)
Definition: Exception_t.cc:16
int x
Definition: Exception_t.cc:12
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
const double e
void func3()
Definition: Exception_t.cc:43
const char * correct[]
Definition: Exception_t.cc:113
int main()
Definition: Exception_t.cc:116
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Thing(int xx)
Definition: Exception_t.cc:11
void func1()
Definition: Exception_t.cc:79
void func2()
Definition: Exception_t.cc:73
static bool * b
Definition: config.cpp:1043
Q_EXPORT QTSManip setfill(int f)
Definition: qtextstream.h:337
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)