OMNeT++ Simulation Library  5.4.1
cexception.h
1 //==========================================================================
2 // CEXCEPTION.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CEXCEPTION_H
17 #define __OMNETPP_CEXCEPTION_H
18 
19 #include <cstdarg>
20 #include <exception>
21 #include <stdexcept>
22 #include "simkerneldefs.h"
23 #include "simtime_t.h"
24 #include "errmsg.h"
25 
26 namespace omnetpp {
27 
28 class cObject;
29 class cComponent;
30 
41 typedef int ErrorCodeInt;
42 
48 class SIM_API cException : public std::exception
49 {
50  protected:
51  ErrorCode errorCode;
52  std::string msg;
53 
54  int simulationStage;
55  eventnumber_t eventNumber;
56  simtime_t simtime;
57 
58  bool hasContext_;
59  std::string contextClassName;
60  std::string contextFullPath;
61  int contextComponentId;
62  int contextComponentKind; // actually cComponent::ComponentKind
63 
69  void init(const cObject *obj, ErrorCode errorcode, const char *fmt, va_list va);
70 
71  // helper for init()
72  void storeContext();
73 
74  // default constructor, for subclasses only.
75  cException();
76 
77  //
78  // Helper, called from cException constructors.
79  //
80  // If an exception occurs in initialization code (during construction of
81  // global objects, before main() is called), there is nobody who could
82  // catch the error, so it would just cause a program abort.
83  // Here we handle this case manually: if cException ctor is invoked before
84  // main() has started, we print the error message and call exit(1).
85  //
86  void exitIfStartupError();
87 
88  public:
96  cException(ErrorCodeInt errcode,...);
97 
101  cException(const char *msg,...);
102 
110  cException(const cObject *where, ErrorCodeInt errcode,...);
111 
117  cException(const cObject *where, const char *msg,...);
118 
123  cException(const cException&);
124 
129  virtual cException *dup() const {return new cException(*this);}
130 
134  virtual ~cException() throw() {}
136 
142  virtual void setMessage(const char *txt) {msg = txt;}
143 
147  virtual void prependMessage(const char *txt) {msg = std::string(txt) + ": " + msg;}
149 
156  virtual bool isError() const {return true;}
157 
161  virtual int getErrorCode() const {return errorCode;}
162 
166  virtual const char *what() const throw() override {return msg.c_str();}
167 
173  virtual std::string getFormattedMessage() const;
174 
181  virtual int getSimulationStage() const {return simulationStage;}
182 
186  virtual eventnumber_t getEventNumber() const {return eventNumber;}
187 
191  virtual simtime_t getSimtime() const {return simtime;}
192 
198  virtual bool hasContext() const {return hasContext_;}
199 
204  virtual const char *getContextClassName() const {return contextClassName.c_str();}
205 
210  virtual const char *getContextFullPath() const {return contextFullPath.c_str();}
211 
218  virtual int getContextComponentId() const {return contextComponentId;}
219 
224  virtual int getContextComponentKind() const {return contextComponentKind;}
226 };
227 
238 class SIM_API cTerminationException : public cException
239 {
240  public:
246  cTerminationException(ErrorCodeInt errcode,...);
247 
251  cTerminationException(const char *msg,...);
252 
258 
263  virtual cTerminationException *dup() const override {return new cTerminationException(*this);}
264 
269  virtual bool isError() const override {return false;}
270 };
271 
282 class SIM_API cRuntimeError : public cException
283 {
284  public:
285  // internal
286  bool displayed = false;
287  protected:
288  // internal
289  void notifyEnvir();
290 
291  public:
292 
298  cRuntimeError(ErrorCodeInt errcode,...);
299 
303  cRuntimeError(const char *msg,...);
304 
312  cRuntimeError(const cObject *where, ErrorCodeInt errcode,...);
313 
319  cRuntimeError(const cObject *where, const char *msg,...);
320 
326 
331  virtual cRuntimeError *dup() const override {return new cRuntimeError(*this);}
332 };
333 
340 class SIM_API cDeleteModuleException : public cException
341 {
342  public:
347 
353 
358  virtual cDeleteModuleException *dup() const override {return new cDeleteModuleException(*this);}
359 
363  virtual bool isError() const override {return false;}
364 };
365 
375 class SIM_API cStackCleanupException : public cException
376 {
377  public:
382 
388 
393  virtual cStackCleanupException *dup() const override {return new cStackCleanupException(*this);}
394 
398  virtual bool isError() const override {return false;}
399 };
400 
401 } // namespace omnetpp
402 
403 
404 #endif
405 
406 
cStackCleanupException(const cStackCleanupException &e)
Definition: cexception.h:387
virtual cDeleteModuleException * dup() const override
Definition: cexception.h:358
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:282
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
virtual cException * dup() const
Definition: cexception.h:129
virtual int getContextComponentKind() const
Definition: cexception.h:224
virtual ~cException()
Definition: cexception.h:134
virtual bool isError() const override
Definition: cexception.h:398
virtual simtime_t getSimtime() const
Definition: cexception.h:191
virtual bool isError() const override
Definition: cexception.h:269
cRuntimeError(const cRuntimeError &e)
Definition: cexception.h:325
virtual int getContextComponentId() const
Definition: cexception.h:218
cDeleteModuleException()
Definition: cexception.h:346
virtual bool isError() const override
Definition: cexception.h:363
virtual const char * getContextClassName() const
Definition: cexception.h:204
virtual cStackCleanupException * dup() const override
Definition: cexception.h:393
virtual int getErrorCode() const
Definition: cexception.h:161
virtual void setMessage(const char *txt)
Definition: cexception.h:142
cDeleteModuleException(const cDeleteModuleException &e)
Definition: cexception.h:352
virtual bool isError() const
Definition: cexception.h:156
virtual int getSimulationStage() const
Definition: cexception.h:181
cTerminationException(const cTerminationException &e)
Definition: cexception.h:257
cStackCleanupException()
Definition: cexception.h:381
virtual void prependMessage(const char *txt)
Definition: cexception.h:147
Thrown when the simulation is completed without error.
Definition: cexception.h:238
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
virtual const char * getContextFullPath() const
Definition: cexception.h:210
Definition: cabstracthistogram.h:21
virtual cTerminationException * dup() const override
Definition: cexception.h:263
virtual eventnumber_t getEventNumber() const
Definition: cexception.h:186
virtual cRuntimeError * dup() const override
Definition: cexception.h:331
virtual const char * what() const override
Definition: cexception.h:166
Exception class.
Definition: cexception.h:48
virtual bool hasContext() const
Definition: cexception.h:198
Used internally when deleting an activity() simple module.
Definition: cexception.h:375
Thrown from cSimpleModule::deleteModule() when the current module is about to be deleted, in order to exit that module immediately.
Definition: cexception.h:340