CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testExceptions.cc File Reference
#include <sstream>
#include <string>
#include "CLHEP/Exceptions/defs.h"
#include "CLHEP/Cast/itos.h"
#include "CLHEP/Exceptions/ZMthrow.h"
#include "CLHEP/Exceptions/ZMexAction.h"
#include "CLHEP/Exceptions/ZMexClassInfo.h"
#include "CLHEP/Exceptions/ZMexSeverity.h"
#include "CLHEP/Exceptions/ZMerrno.h"
#include "CLHEP/Exceptions/ZMexception.h"
#include <iostream>

Go to the source code of this file.

Functions

 ZMexStandardDefinition (ZMexception, ZMxTest)
 
 ZMexStandardDefinition (ZMxTest, ZMxInfo)
 
 ZMexStandardDefinition (ZMxTest, ZMxGoof)
 
 ZMexStandardDefinition (ZMxGoof, ZMxOops)
 
 ZMexStandardDefinition (ZMxGoof, ZMxBooBoo)
 
void display (const ZMexception *ex)
 
int main ()
 

Variables

const string QUOTE = "\""
 
const string NEWLINE1 = "\n"
 
const string NEWLINE2 = "\n\n"
 

Function Documentation

◆ display()

void display ( const ZMexception ex)

Definition at line 88 of file testExceptions.cc.

88 {
89
91 + ex->name() + ": " + QUOTE + ex->message() + QUOTE + NEWLINE1
92 + " " + (ex->wasThrown() ? "thrown" : "ignored")
93 + " by " + ex->handlerUsed() + "()" + NEWLINE1
94 );
95
96}
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
ZMexLogger & ZMlogger()
Definition: ZMexception.cc:70
const string QUOTE
const string NEWLINE1

Referenced by main().

◆ main()

int main ( )

Definition at line 99 of file testExceptions.cc.

99 {
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 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
std::string itos(long i)
Definition: itos.cc:18
ZMerrnoList ZMerrno
Definition: ZMerrno.cc:40
const string NEWLINE2
void display(const ZMexception *ex)

◆ ZMexStandardDefinition() [1/5]

ZMexStandardDefinition ( ZMexception  ,
ZMxTest   
)

◆ ZMexStandardDefinition() [2/5]

ZMexStandardDefinition ( ZMxGoof  ,
ZMxBooBoo   
)

◆ ZMexStandardDefinition() [3/5]

ZMexStandardDefinition ( ZMxGoof  ,
ZMxOops   
)

◆ ZMexStandardDefinition() [4/5]

ZMexStandardDefinition ( ZMxTest  ,
ZMxGoof   
)

◆ ZMexStandardDefinition() [5/5]

ZMexStandardDefinition ( ZMxTest  ,
ZMxInfo   
)

Variable Documentation

◆ NEWLINE1

const string NEWLINE1 = "\n"

Definition at line 85 of file testExceptions.cc.

Referenced by display(), and main().

◆ NEWLINE2

const string NEWLINE2 = "\n\n"

Definition at line 86 of file testExceptions.cc.

Referenced by main().

◆ QUOTE

const string QUOTE = "\""

Definition at line 84 of file testExceptions.cc.

Referenced by display().