OMNeT++ Simulation Library  6.0.3
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 std::string& msg);
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  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
103  cException(const char *msg,...);
104 
112  cException(const cObject *where, ErrorCodeInt errcode,...);
113 
119  _OPP_GNU_ATTRIBUTE(format(printf, 3, 4))
120  cException(const cObject *where, const char *msg,...);
121 
126  cException(const cException&) = default;
127 
132  virtual cException *dup() const {return new cException(*this);}
133 
137  virtual ~cException() throw() {}
139 
145  virtual void setMessage(const char *txt) {msg = txt;}
146 
150  virtual void prependMessage(const char *txt) {msg = std::string(txt) + ": " + msg;}
152 
159  virtual bool isError() const {return true;}
160 
164  virtual int getErrorCode() const {return errorCode;}
165 
169  virtual const char *what() const throw() override {return msg.c_str();}
170 
176  virtual std::string getFormattedMessage() const;
177 
184  virtual int getSimulationStage() const {return simulationStage;}
185 
189  virtual eventnumber_t getEventNumber() const {return eventNumber;}
190 
194  virtual simtime_t getSimtime() const {return simtime;}
195 
201  virtual bool hasContext() const {return hasContext_;}
202 
207  virtual const char *getContextClassName() const {return contextClassName.c_str();}
208 
213  virtual const char *getContextFullPath() const {return contextFullPath.c_str();}
214 
221  virtual int getContextComponentId() const {return contextComponentId;}
222 
227  virtual int getContextComponentKind() const {return contextComponentKind;}
229 };
230 
241 class SIM_API cTerminationException : public cException
242 {
243  public:
249  cTerminationException(ErrorCodeInt errcode,...);
250 
254  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
255  cTerminationException(const char *msg,...);
256 
261  cTerminationException(const cTerminationException& e) = default;
262 
267  virtual cTerminationException *dup() const override {return new cTerminationException(*this);}
268 
273  virtual bool isError() const override {return false;}
274 };
275 
286 class SIM_API cRuntimeError : public cException
287 {
288  public:
289  // internal
290  bool displayed = false;
291  protected:
292  // internal
293  void notifyEnvir();
294 
295  public:
296 
302  cRuntimeError(ErrorCodeInt errcode,...);
303 
307  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
308  cRuntimeError(const char *msg,...);
309 
317  cRuntimeError(const cObject *where, ErrorCodeInt errcode,...);
318 
324  _OPP_GNU_ATTRIBUTE(format(printf, 3, 4))
325  cRuntimeError(const cObject *where, const char *msg,...);
326 
330  cRuntimeError(const std::exception& e, const char *location);
331 
337 
342  virtual cRuntimeError *dup() const override {return new cRuntimeError(*this);}
343 };
344 
351 class SIM_API cDeleteModuleException : public cException
352 {
353  protected:
354  cModule *toDelete;
355 
356  public:
360  cDeleteModuleException(cModule *toDelete) : cException(), toDelete(toDelete) {}
361 
367 
372  virtual cDeleteModuleException *dup() const override {return new cDeleteModuleException(*this);}
373 
377  virtual cModule *getModuleToDelete() const {return toDelete;}
378 
382  virtual bool isError() const override {return false;}
383 };
384 
394 class SIM_API cStackCleanupException : public cException
395 {
396  public:
401 
407 
412  virtual cStackCleanupException *dup() const override {return new cStackCleanupException(*this);}
413 
417  virtual bool isError() const override {return false;}
418 };
419 
420 } // namespace omnetpp
421 
422 
423 #endif
424 
425 
omnetpp::cException::prependMessage
virtual void prependMessage(const char *txt)
Definition: cexception.h:150
omnetpp::cException
Exception class.
Definition: cexception.h:49
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cException::hasContext
virtual bool hasContext() const
Definition: cexception.h:201
omnetpp::cException::getSimtime
virtual simtime_t getSimtime() const
Definition: cexception.h:194
omnetpp::cDeleteModuleException::isError
virtual bool isError() const override
Definition: cexception.h:382
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cException::isError
virtual bool isError() const
Definition: cexception.h:159
omnetpp::cRuntimeError::cRuntimeError
cRuntimeError(const cRuntimeError &e)
Definition: cexception.h:336
omnetpp::cDeleteModuleException::getModuleToDelete
virtual cModule * getModuleToDelete() const
Definition: cexception.h:377
omnetpp::cException::getContextComponentId
virtual int getContextComponentId() const
Definition: cexception.h:221
omnetpp::cException::setMessage
virtual void setMessage(const char *txt)
Definition: cexception.h:145
omnetpp::cException::getContextComponentKind
virtual int getContextComponentKind() const
Definition: cexception.h:227
omnetpp::cRuntimeError::dup
virtual cRuntimeError * dup() const override
Definition: cexception.h:342
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::cTerminationException
Thrown when the simulation is completed without error.
Definition: cexception.h:241
omnetpp::cStackCleanupException
Used internally when deleting an activity() simple module.
Definition: cexception.h:394
omnetpp::cStackCleanupException::cStackCleanupException
cStackCleanupException()
Definition: cexception.h:400
omnetpp::cDeleteModuleException
Thrown from deleteModule() when the active activity() module is about to be deleted,...
Definition: cexception.h:351
omnetpp::eventnumber_t
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
omnetpp::cStackCleanupException::dup
virtual cStackCleanupException * dup() const override
Definition: cexception.h:412
omnetpp::cException::getContextClassName
virtual const char * getContextClassName() const
Definition: cexception.h:207
omnetpp::cException::~cException
virtual ~cException()
Definition: cexception.h:137
omnetpp::cTerminationException::dup
virtual cTerminationException * dup() const override
Definition: cexception.h:267
omnetpp::cException::getEventNumber
virtual eventnumber_t getEventNumber() const
Definition: cexception.h:189
omnetpp::cTerminationException::isError
virtual bool isError() const override
Definition: cexception.h:273
omnetpp::cException::getErrorCode
virtual int getErrorCode() const
Definition: cexception.h:164
omnetpp::cDeleteModuleException::dup
virtual cDeleteModuleException * dup() const override
Definition: cexception.h:372
omnetpp::cException::getContextFullPath
virtual const char * getContextFullPath() const
Definition: cexception.h:213
omnetpp::cStackCleanupException::isError
virtual bool isError() const override
Definition: cexception.h:417
omnetpp::cException::dup
virtual cException * dup() const
Definition: cexception.h:132
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::cException::what
virtual const char * what() const override
Definition: cexception.h:169
omnetpp::cDeleteModuleException::cDeleteModuleException
cDeleteModuleException(cModule *toDelete)
Definition: cexception.h:360
omnetpp::cException::getSimulationStage
virtual int getSimulationStage() const
Definition: cexception.h:184