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
21#include "CLHEP/Cast/itos.h"
25
26
27using 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
55ZMexClassInfo ZMxTest::_classInfo(
56 "ZMxTest", "Test", ZMexSEVERE );
57
58ZMexStandardDefinition( ZMxTest, ZMxInfo );
59ZMexClassInfo ZMxInfo::_classInfo(
60 "ZMxInfo", "Test", ZMexINFO );
61
62ZMexStandardDefinition( ZMxTest, ZMxGoof );
63ZMexClassInfo ZMxGoof::_classInfo(
64 "ZMxGoof", "Test", ZMexERROR );
65
66ZMexStandardDefinition( ZMxGoof, ZMxOops );
67ZMexClassInfo ZMxOops::_classInfo(
68 "ZMxOops", "Test", ZMexWARNING );
69
70ZMexStandardDefinition( ZMxGoof, ZMxBooBoo );
71ZMexClassInfo ZMxBooBoo::_classInfo(
72 "ZMxBooBoo", "Test", ZMexFATAL, ZMexIgnoreAlways() );
73
74
75// ----------
76// Define output formats, etc
77// ----------
78
79const string QUOTE = "\"";
80const string NEWLINE1 = "\n";
81const string NEWLINE2 = "\n\n";
82
83void 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
94int main() {
95 unsigned int k;
96
97 // ----------
98 // Begin testing, check out basic logger:
99 // ----------
101 ZMlogger().emit( "---------- Begin testing: ----------\n" );
103 ZMlogger().emit( "This message checks out basic logger behavior\n" );
104
105
106 // ----------
107 // Test default informational behavior:
108 // ----------
110 ZMlogger().emit(
111 "---------- Testing default informational behavior: ----------\n" );
113 ZMthrow( ZMxInfo( "Testing default informational exception behavior" ) );
114
115 // ----------
116 // Test default informational behavior using an ostringstream
117 // ----------
118
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 ) {
126 if ( k > 0 )
127 oss << ", ";
128 oss << k;
129 ZMthrow( ZMxOops( oss ) );
130 }
131
132 // ----------
133 // Test default ignoring behavior 3 times:
134 // ----------
136 ZMlogger().emit(
137 "---------- Testing default ignoring behavior 3 times: ----------\n" );
138 for ( k = 0; k < 3; ++k ) {
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 // ----------
149 ZMlogger().emit(
150 "---------- Testing defined ignoring behavior 3 times: ----------\n" );
151 for ( k = 0; k < 3; ++k ) {
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 // ----------
161 ZMlogger().emit(
162 "---------- Testing default throwing behavior 3 times: ----------\n" );
163 for ( k = 0; k < 3; ++k ) {
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 // ----------
183 ZMlogger().emit(
184 "---------- Testing forced throwing behavior 3 times: ----------\n" );
185 ZMxBooBoo::setHandler( ZMexThrowAlways() );
186 for ( k = 0; k < 3; ++k ) {
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 // ----------
205 ZMlogger().emit(
206 "---------- Testing scheduled throwing behavior 3 times: ----------\n" );
207 ZMxBooBoo::setHandler( ZMexIgnoreNextN( 2 ) );
208 for ( k = 0; k < 3; ++k ) {
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 // ----------
231 ZMlogger().emit( "---------- Test exception history: ----------\n" );
232
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 );
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()
#define ZMexStandardDefinition(Parent, Class)
#define ZMthrow(userExcept)
std::string itos(long i)
Definition itos.cc:18
unsigned int setMax(unsigned int limit)
Definition ZMerrno.cc:146
unsigned int size() const
const ZMexception * get(unsigned int k=0) const
Definition ZMerrno.cc:94
ZMexLogResult emit(const ZMexception &exc)
std::string handlerUsed() const
std::string message() const
bool wasThrown() const
virtual std::string name() const
ZMexLogger & ZMlogger()
ZMerrnoList ZMerrno
const string QUOTE
const string NEWLINE2
const string NEWLINE1
int main()
void display(const ZMexception *ex)