CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
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"
22#include "CLHEP/Exceptions/ZMthrow.h"
23#include "CLHEP/Exceptions/ZMexAction.h"
24#include "CLHEP/Exceptions/ZMexClassInfo.h"
25#include "CLHEP/Exceptions/ZMexSeverity.h"
26#include "CLHEP/Exceptions/ZMerrno.h"
27#include "CLHEP/Exceptions/ZMexception.h"
28
29#include <iostream>
30
31
32using namespace zmex;
33
34// ----------
35// In case our compilation environment does not support exceptions:
36//
37// Since this program tests several cases, including several actual throws,
38// we assume that the DEFECT_NO_EXCEPTIONS version of the ZMthrow macro
39// will use abort() and so override abort()'s behavior in order to keep
40// going for multiple testing herein: (earlier we used exit(), which we
41// also overide)
42// ----------
43
44#ifdef DEFECT_NO_EXCEPTIONS
45
46#define exit( x ) \
47 ZMlogger().emit( "Note: exception would have been correctly thrown here\n" );
48
49#define abort() \
50 ZMlogger().emit( "Note: exception would have been correctly thrown here\n" );
51
52#endif // DEFECT_NO_EXCEPTIONS
53
54
55// ----------
56// Define exception classes and default behaviors:
57// ----------
58
60ZMexClassInfo ZMxTest::_classInfo(
61 "ZMxTest", "Test", ZMexSEVERE );
62
63ZMexStandardDefinition( ZMxTest, ZMxInfo );
64ZMexClassInfo ZMxInfo::_classInfo(
65 "ZMxInfo", "Test", ZMexINFO );
66
67ZMexStandardDefinition( ZMxTest, ZMxGoof );
68ZMexClassInfo ZMxGoof::_classInfo(
69 "ZMxGoof", "Test", ZMexERROR );
70
71ZMexStandardDefinition( ZMxGoof, ZMxOops );
72ZMexClassInfo ZMxOops::_classInfo(
73 "ZMxOops", "Test", ZMexWARNING );
74
75ZMexStandardDefinition( ZMxGoof, ZMxBooBoo );
76ZMexClassInfo ZMxBooBoo::_classInfo(
77 "ZMxBooBoo", "Test", ZMexFATAL, ZMexIgnoreAlways() );
78
79
80// ----------
81// Define output formats, etc
82// ----------
83
84const string QUOTE = "\"";
85const string NEWLINE1 = "\n";
86const string NEWLINE2 = "\n\n";
87
88void display( const ZMexception * ex ) {
89
91 + ex->name() + ": " + QUOTE + ex->message() + QUOTE + NEWLINE1
92 + " " + (ex->wasThrown() ? "thrown" : "ignored")
93 + " by " + ex->handlerUsed() + "()" + NEWLINE1
94 );
95
96}
97
98
99int main() {
100 unsigned int k;
101
102 // ----------
103 // Begin testing, check out basic logger:
104 // ----------
106 ZMlogger().emit( "---------- Begin testing: ----------\n" );
108 ZMlogger().emit( "This message checks out basic logger behavior\n" );
109
110
111 // ----------
112 // Test default informational behavior:
113 // ----------
115 ZMlogger().emit(
116 "---------- Testing default informational behavior: ----------\n" );
118 ZMthrow( ZMxInfo( "Testing default informational exception behavior" ) );
119
120 // ----------
121 // Test default informational behavior using an ostringstream
122 // ----------
123
125 ZMlogger().emit(
126 "---------- Testing default ostringstream behavior 3 times: ----------\n" );
127 std::ostringstream oss;
128 oss << "Accumulated numbers are ";
129 for ( k = 0; k < 3; ++k ) {
131 if ( k > 0 )
132 oss << ", ";
133 oss << k;
134 ZMthrow( ZMxOops( oss ) );
135 }
136
137 // ----------
138 // Test default ignoring behavior 3 times:
139 // ----------
141 ZMlogger().emit(
142 "---------- Testing default ignoring behavior 3 times: ----------\n" );
143 for ( k = 0; k < 3; ++k ) {
145 string testMesg = "Testing default exception ignoring behavior #";
146 ZMthrow( ZMxOops( testMesg + itos(k+1) ) );
147 }
148
149
150 // ----------
151 // Test defined ignoring behavior 3 times:
152 // ----------
154 ZMlogger().emit(
155 "---------- Testing defined ignoring behavior 3 times: ----------\n" );
156 for ( k = 0; k < 3; ++k ) {
158 string testMesg = "Testing defined exception ignoring behavior #";
159 ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
160 }
161
162 // ----------
163 // Test default throwing behavior 3 times:
164 // ----------
166 ZMlogger().emit(
167 "---------- Testing default throwing behavior 3 times: ----------\n" );
168 for ( k = 0; k < 3; ++k ) {
170 string testMesg = "Testing default exception throwing behavior #";
171#ifndef DEFECT_NO_EXCEPTIONS
172 try {
173#endif
174 ZMthrow( ZMxGoof( testMesg + itos(k+1) ) );
175#ifndef DEFECT_NO_EXCEPTIONS
176 }
177 catch ( ZMexception & e ) {
178 std::cerr << "Caught: " << e.name() << "\n";
179 }
180#endif
181 }
182
183
184 // ----------
185 // Test forced throwing behavior 3 times:
186 // ----------
188 ZMlogger().emit(
189 "---------- Testing forced throwing behavior 3 times: ----------\n" );
190 ZMxBooBoo::setHandler( ZMexThrowAlways() );
191 for ( k = 0; k < 3; ++k ) {
193 string testMesg = "Testing forced exception throwing behavior #";
194#ifndef DEFECT_NO_EXCEPTIONS
195 try {
196#endif
197 ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
198#ifndef DEFECT_NO_EXCEPTIONS
199 }
200 catch ( ZMexception & e ) {
201 std::cerr << "Caught: " << e.name() << "\n";
202 }
203#endif
204 }
205
206 // ----------
207 // Test scheduled throwing behavior 3 times:
208 // ----------
210 ZMlogger().emit(
211 "---------- Testing scheduled throwing behavior 3 times: ----------\n" );
212 ZMxBooBoo::setHandler( ZMexIgnoreNextN( 2 ) );
213 for ( k = 0; k < 3; ++k ) {
215 string testMesg = "Testing scheduled exception throwing behavior #";
216#ifndef DEFECT_NO_EXCEPTIONS
217 try {
218#endif
219 ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
220#ifndef DEFECT_NO_EXCEPTIONS
221 }
222 catch ( ZMexception & e ) {
223 std::cerr << "Caught: " << e.name() << "\n";
224 }
225#endif
226 }
227
228
229 //ZMxColumn::logNMore( 4 );
230
231
232 // ----------
233 // Test exception history:
234 // ----------
236 ZMlogger().emit( "---------- Test exception history: ----------\n" );
237
239 ZMlogger().emit( string( "The following " )
240 + itos( ZMerrno.size() )
241 + " exceptions were recorded (most recent first):\n"
242 );
243 for ( k = 0; k < ZMerrno.size(); ++k )
244 display( ZMerrno.get( k ) );
245
246 const int NEWMAX = 4;
247 ZMerrno.setMax( NEWMAX );
249 ZMlogger().emit( string("ZMerrno.setMax( ")
250 + itos( NEWMAX )
251 + " );"
252 );
253
254 ZMlogger().emit( string( " we now have record of these " )
255 + itos(ZMerrno.size())
256 + " exceptions:\n"
257 );
258 for ( k = 0; k < ZMerrno.size(); ++k )
259 display( ZMerrno.get( k ) );
260
261 // ----------
262 // Done, go home
263 // ----------
264 return 0;
265
266} // main()
#define ZMexStandardDefinition(Parent, Class)
Definition: ZMexception.h:523
#define ZMthrow(userExcept)
Definition: ZMthrow.h:97
unsigned int setMax(unsigned int limit)
Definition: ZMerrno.cc:147
unsigned int size() const
const ZMexception * get(unsigned int k=0) const
Definition: ZMerrno.cc:95
ZMexLogResult emit(const ZMexception &exc)
Definition: ZMexLogger.cc:255
std::string handlerUsed() const
std::string message() const
bool wasThrown() const
virtual std::string name() const
Definition: ZMexception.cc:106
std::string itos(long i)
Definition: itos.cc:18
Definition: ZMerrno.h:52
ZMexLogger & ZMlogger()
Definition: ZMexception.cc:70
@ ZMexWARNING
Definition: ZMexSeverity.h:41
@ ZMexSEVERE
Definition: ZMexSeverity.h:53
@ ZMexERROR
Definition: ZMexSeverity.h:46
@ ZMexFATAL
Definition: ZMexSeverity.h:57
@ ZMexINFO
Definition: ZMexSeverity.h:37
ZMerrnoList ZMerrno
Definition: ZMerrno.cc:40
const string QUOTE
const string NEWLINE2
const string NEWLINE1
int main()
void display(const ZMexception *ex)