CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testExceptions.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // testExceptions.cc - test the DEFECT_NO_EXCEPTIONS version of the Exceptions package
4 //
5 // History:
6 // 19-Dec-1997 WEB Initial draft; redefining exit() based on an idea
7 // by Philippe Canal
8 // 04-Mar-1998 WEB Minor grammar update
9 // 15-Jun-1998 WEB Added namespace support
10 // 26-Jun-2001 MF Tested ctor of ZMexception from ostringstream
11 // 12-Dec-2001 WEB Avoid signed/unsigned comparison warnings
12 // 12-Jun-2002 WEB Rearrange to ensure correct ZMthrow definition
13 //
14 // ----------------------------------------------------------------------
15 
16 #include <sstream>
17 #include <string>
18  using std::string;
19 
20 #include "CLHEP/Exceptions/defs.h"
21 #include "CLHEP/Cast/itos.h"
25 
26 
27 using namespace zmex;
28 
29 // ----------
30 // In case our compilation environment does not support exceptions:
31 //
32 // Since this program tests several cases, including several actual throws,
33 // we assume that the DEFECT_NO_EXCEPTIONS version of the ZMthrow macro
34 // will use abort() and so override abort()'s behavior in order to keep
35 // going for multiple testing herein: (earlier we used exit(), which we
36 // also overide)
37 // ----------
38 
39 #ifdef DEFECT_NO_EXCEPTIONS
40 
41 #define exit( x ) \
42  ZMlogger().emit( "Note: exception would have been correctly thrown here\n" );
43 
44 #define abort() \
45  ZMlogger().emit( "Note: exception would have been correctly thrown here\n" );
46 
47 #endif // DEFECT_NO_EXCEPTIONS
48 
49 
50 // ----------
51 // Define exception classes and default behaviors:
52 // ----------
53 
55 ZMexClassInfo ZMxTest::_classInfo(
56  "ZMxTest", "Test", ZMexSEVERE );
57 
58 ZMexStandardDefinition( ZMxTest, ZMxInfo );
59 ZMexClassInfo ZMxInfo::_classInfo(
60  "ZMxInfo", "Test", ZMexINFO );
61 
62 ZMexStandardDefinition( ZMxTest, ZMxGoof );
63 ZMexClassInfo ZMxGoof::_classInfo(
64  "ZMxGoof", "Test", ZMexERROR );
65 
66 ZMexStandardDefinition( ZMxGoof, ZMxOops );
67 ZMexClassInfo ZMxOops::_classInfo(
68  "ZMxOops", "Test", ZMexWARNING );
69 
70 ZMexStandardDefinition( ZMxGoof, ZMxBooBoo );
71 ZMexClassInfo ZMxBooBoo::_classInfo(
72  "ZMxBooBoo", "Test", ZMexFATAL, ZMexIgnoreAlways() );
73 
74 
75 // ----------
76 // Define output formats, etc
77 // ----------
78 
79 const string QUOTE = "\"";
80 const string NEWLINE1 = "\n";
81 const string NEWLINE2 = "\n\n";
82 
83 void display( const ZMexception * ex ) {
84 
86  + ex->name() + ": " + QUOTE + ex->message() + QUOTE + NEWLINE1
87  + " " + (ex->wasThrown() ? "thrown" : "ignored")
88  + " by " + ex->handlerUsed() + "()" + NEWLINE1
89  );
90 
91 }
92 
93 
94 int main() {
95  unsigned int k;
96 
97  // ----------
98  // Begin testing, check out basic logger:
99  // ----------
100  ZMlogger().emit( NEWLINE1 );
101  ZMlogger().emit( "---------- Begin testing: ----------\n" );
102  ZMlogger().emit( NEWLINE1 );
103  ZMlogger().emit( "This message checks out basic logger behavior\n" );
104 
105 
106  // ----------
107  // Test default informational behavior:
108  // ----------
109  ZMlogger().emit( NEWLINE2 );
110  ZMlogger().emit(
111  "---------- Testing default informational behavior: ----------\n" );
112  ZMlogger().emit( NEWLINE1 );
113  ZMthrow( ZMxInfo( "Testing default informational exception behavior" ) );
114 
115  // ----------
116  // Test default informational behavior using an ostringstream
117  // ----------
118 
119  ZMlogger().emit( NEWLINE2 );
120  ZMlogger().emit(
121  "---------- Testing default ostringstream behavior 3 times: ----------\n" );
122  std::ostringstream oss;
123  oss << "Accumulated numbers are ";
124  for ( k = 0; k < 3; ++k ) {
125  ZMlogger().emit( NEWLINE1 );
126  if ( k > 0 )
127  oss << ", ";
128  oss << k;
129  ZMthrow( ZMxOops( oss ) );
130  }
131 
132  // ----------
133  // Test default ignoring behavior 3 times:
134  // ----------
135  ZMlogger().emit( NEWLINE2 );
136  ZMlogger().emit(
137  "---------- Testing default ignoring behavior 3 times: ----------\n" );
138  for ( k = 0; k < 3; ++k ) {
139  ZMlogger().emit( NEWLINE1 );
140  string testMesg = "Testing default exception ignoring behavior #";
141  ZMthrow( ZMxOops( testMesg + itos(k+1) ) );
142  }
143 
144 
145  // ----------
146  // Test defined ignoring behavior 3 times:
147  // ----------
148  ZMlogger().emit( NEWLINE2 );
149  ZMlogger().emit(
150  "---------- Testing defined ignoring behavior 3 times: ----------\n" );
151  for ( k = 0; k < 3; ++k ) {
152  ZMlogger().emit( NEWLINE1 );
153  string testMesg = "Testing defined exception ignoring behavior #";
154  ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
155  }
156 
157  // ----------
158  // Test default throwing behavior 3 times:
159  // ----------
160  ZMlogger().emit( NEWLINE2 );
161  ZMlogger().emit(
162  "---------- Testing default throwing behavior 3 times: ----------\n" );
163  for ( k = 0; k < 3; ++k ) {
164  ZMlogger().emit( NEWLINE1 );
165  string testMesg = "Testing default exception throwing behavior #";
166 #ifndef DEFECT_NO_EXCEPTIONS
167  try {
168 #endif
169  ZMthrow( ZMxGoof( testMesg + itos(k+1) ) );
170 #ifndef DEFECT_NO_EXCEPTIONS
171  }
172  catch ( ZMexception & e ) {
173  std::cerr << "Caught: " << e.name() << "\n";
174  }
175 #endif
176  }
177 
178 
179  // ----------
180  // Test forced throwing behavior 3 times:
181  // ----------
182  ZMlogger().emit( NEWLINE2 );
183  ZMlogger().emit(
184  "---------- Testing forced throwing behavior 3 times: ----------\n" );
185  ZMxBooBoo::setHandler( ZMexThrowAlways() );
186  for ( k = 0; k < 3; ++k ) {
187  ZMlogger().emit( NEWLINE1 );
188  string testMesg = "Testing forced exception throwing behavior #";
189 #ifndef DEFECT_NO_EXCEPTIONS
190  try {
191 #endif
192  ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
193 #ifndef DEFECT_NO_EXCEPTIONS
194  }
195  catch ( ZMexception & e ) {
196  std::cerr << "Caught: " << e.name() << "\n";
197  }
198 #endif
199  }
200 
201  // ----------
202  // Test scheduled throwing behavior 3 times:
203  // ----------
204  ZMlogger().emit( NEWLINE2 );
205  ZMlogger().emit(
206  "---------- Testing scheduled throwing behavior 3 times: ----------\n" );
207  ZMxBooBoo::setHandler( ZMexIgnoreNextN( 2 ) );
208  for ( k = 0; k < 3; ++k ) {
209  ZMlogger().emit( NEWLINE1 );
210  string testMesg = "Testing scheduled exception throwing behavior #";
211 #ifndef DEFECT_NO_EXCEPTIONS
212  try {
213 #endif
214  ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
215 #ifndef DEFECT_NO_EXCEPTIONS
216  }
217  catch ( ZMexception & e ) {
218  std::cerr << "Caught: " << e.name() << "\n";
219  }
220 #endif
221  }
222 
223 
224  //ZMxColumn::logNMore( 4 );
225 
226 
227  // ----------
228  // Test exception history:
229  // ----------
230  ZMlogger().emit( NEWLINE2 );
231  ZMlogger().emit( "---------- Test exception history: ----------\n" );
232 
233  ZMlogger().emit( NEWLINE1 );
234  ZMlogger().emit( string( "The following " )
235  + itos( ZMerrno.size() )
236  + " exceptions were recorded (most recent first):\n"
237  );
238  for ( k = 0; k < ZMerrno.size(); ++k )
239  display( ZMerrno.get( k ) );
240 
241  const int NEWMAX = 4;
242  ZMerrno.setMax( NEWMAX );
243  ZMlogger().emit( NEWLINE1 );
244  ZMlogger().emit( string("ZMerrno.setMax( ")
245  + itos( NEWMAX )
246  + " );"
247  );
248 
249  ZMlogger().emit( string( " we now have record of these " )
250  + itos(ZMerrno.size())
251  + " exceptions:\n"
252  );
253  for ( k = 0; k < ZMerrno.size(); ++k )
254  display( ZMerrno.get( k ) );
255 
256  // ----------
257  // Done, go home
258  // ----------
259  return 0;
260 
261 } // main()
const string NEWLINE1
std::string message() const
ZMexLogResult emit(const ZMexception &exc)
Definition: ZMexLogger.cc:252
ZMexLogger & ZMlogger()
Definition: ZMexception.cc:68
void display(const ZMexception *ex)
std::string itos(long i)
Definition: itos.cc:18
const string QUOTE
unsigned int size() const
unsigned int setMax(unsigned int limit)
Definition: ZMerrno.cc:146
bool wasThrown() const
const string NEWLINE2
#define ZMthrow(userExcept)
ZMerrnoList ZMerrno
ZMexStandardDefinition(ZMexception, ZMxTest)
virtual std::string name() const
Definition: ZMexception.cc:104
int main()
std::string handlerUsed() const
const ZMexception * get(unsigned int k=0) const
Definition: ZMerrno.cc:94