OMNeT++ Simulation Library  5.6.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 class cModule;
31 
42 typedef int ErrorCodeInt;
43 
49 class SIM_API cException : public std::exception
50 {
51  protected:
52  ErrorCode errorCode;
53  std::string msg;
54 
55  int simulationStage;
56  eventnumber_t eventNumber;
57  simtime_t simtime;
58 
59  bool hasContext_;
60  std::string contextClassName;
61  std::string contextFullPath;
62  int contextComponentId;
63  int contextComponentKind; // actually cComponent::ComponentKind
64 
70  void init(const cObject *obj, ErrorCode errorcode, const char *fmt, va_list va);
71 
72  // helper for init()
73  void storeContext();
74 
75  // default constructor, for subclasses only.
76  cException();
77 
78  //
79  // Helper, called from cException constructors.
80  //
81  // If an exception occurs in initialization code (during construction of
82  // global objects, before main() is called), there is nobody who could
83  // catch the error, so it would just cause a program abort.
84  // Here we handle this case manually: if cException ctor is invoked before
85  // main() has started, we print the error message and call exit(1).
86  //
87  void exitIfStartupError();
88 
89  public:
97  cException(ErrorCodeInt errcode,...);
98 
102  cException(const char *msg,...);
103 
111  cException(const cObject *where, ErrorCodeInt errcode,...);
112 
118  cException(const cObject *where, const char *msg,...);
119 
124  cException(const cException&);
125 
130  virtual cException *dup() const {return new cException(*this);}
131 
135  virtual ~cException() throw() {}
137 
143  virtual void setMessage(const char *txt) {msg = txt;}
144 
148  virtual void prependMessage(const char *txt) {msg = std::string(txt) + ": " + msg;}
150 
157  virtual bool isError() const {return true;}
158 
162  virtual int getErrorCode() const {return errorCode;}
163 
167  virtual const char *what() const throw() override {return msg.c_str();}
168 
174  virtual std::string getFormattedMessage() const;
175 
182  virtual int getSimulationStage() const {return simulationStage;}
183 
187  virtual eventnumber_t getEventNumber() const {return eventNumber;}
188 
192  virtual simtime_t getSimtime() const {return simtime;}
193 
199  virtual bool hasContext() const {return hasContext_;}
200 
205  virtual const char *getContextClassName() const {return contextClassName.c_str();}
206 
211  virtual const char *getContextFullPath() const {return contextFullPath.c_str();}
212 
219  virtual int getContextComponentId() const {return contextComponentId;}
220 
225  virtual int getContextComponentKind() const {return contextComponentKind;}
227 };
228 
239 class SIM_API cTerminationException : public cException
240 {
241  public:
247  cTerminationException(ErrorCodeInt errcode,...);
248 
252  cTerminationException(const char *msg,...);
253 
259 
264  virtual cTerminationException *dup() const override {return new cTerminationException(*this);}
265 
270  virtual bool isError() const override {return false;}
271 };
272 
283 class SIM_API cRuntimeError : public cException
284 {
285  public:
286  // internal
287  bool displayed = false;
288  protected:
289  // internal
290  void notifyEnvir();
291 
292  public:
293 
299  cRuntimeError(ErrorCodeInt errcode,...);
300 
304  cRuntimeError(const char *msg,...);
305 
313  cRuntimeError(const cObject *where, ErrorCodeInt errcode,...);
314 
320  cRuntimeError(const cObject *where, const char *msg,...);
321 
327 
332  virtual cRuntimeError *dup() const override {return new cRuntimeError(*this);}
333 };
334 
341 class SIM_API cDeleteModuleException : public cException
342 {
343  protected:
344  cModule *toDelete;
345 
346  public:
350  cDeleteModuleException(cModule *toDelete) : cException(), toDelete(toDelete) {}
351 
356  cDeleteModuleException(const cDeleteModuleException& e) : cException(e), toDelete(e.toDelete) {}
357 
362  virtual cDeleteModuleException *dup() const override {return new cDeleteModuleException(*this);}
363 
367  virtual cModule *getModuleToDelete() const {return toDelete;}
368 
372  virtual bool isError() const override {return false;}
373 };
374 
384 class SIM_API cStackCleanupException : public cException
385 {
386  public:
391 
397 
402  virtual cStackCleanupException *dup() const override {return new cStackCleanupException(*this);}
403 
407  virtual bool isError() const override {return false;}
408 };
409 
410 } // namespace omnetpp
411 
412 
413 #endif
414 
415 
cStackCleanupException(const cStackCleanupException &e)
Definition: cexception.h:396
virtual cDeleteModuleException * dup() const override
Definition: cexception.h:362
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:283
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()
Definition: cexception.h:135
virtual bool isError() const override
Definition: cexception.h:407
virtual bool isError() const override
Definition: cexception.h:270
virtual int getContextComponentKind() const
Definition: cexception.h:225
cRuntimeError(const cRuntimeError &e)
Definition: cexception.h:326
virtual const char * getContextClassName() const
Definition: cexception.h:205
virtual const char * getContextFullPath() const
Definition: cexception.h:211
virtual bool isError() const override
Definition: cexception.h:372
virtual cStackCleanupException * dup() const override
Definition: cexception.h:402
This class represents modules in the simulation.
Definition: cmodule.h:47
virtual void setMessage(const char *txt)
Definition: cexception.h:143
cDeleteModuleException(const cDeleteModuleException &e)
Definition: cexception.h:356
cTerminationException(const cTerminationException &e)
Definition: cexception.h:258
virtual int getSimulationStage() const
Definition: cexception.h:182
cStackCleanupException()
Definition: cexception.h:390
virtual int getContextComponentId() const
Definition: cexception.h:219
virtual void prependMessage(const char *txt)
Definition: cexception.h:148
Thrown when the simulation is completed without error.
Definition: cexception.h:239
virtual bool hasContext() const
Definition: cexception.h:199
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 cException * dup() const
Definition: cexception.h:130
virtual bool isError() const
Definition: cexception.h:157
Definition: cabstracthistogram.h:21
virtual cTerminationException * dup() const override
Definition: cexception.h:264
virtual cRuntimeError * dup() const override
Definition: cexception.h:332
virtual const char * what() const override
Definition: cexception.h:167
virtual cModule * getModuleToDelete() const
Definition: cexception.h:367
Exception class.
Definition: cexception.h:49
virtual eventnumber_t getEventNumber() const
Definition: cexception.h:187
virtual int getErrorCode() const
Definition: cexception.h:162
cDeleteModuleException(cModule *toDelete)
Definition: cexception.h:350
Used internally when deleting an activity() simple module.
Definition: cexception.h:384
virtual simtime_t getSimtime() const
Definition: cexception.h:192
Thrown from deleteModule() when the active activity() module is about to be deleted, in order to exit that module immediately.
Definition: cexception.h:341