OMNeT++ API 6.2.0
Discrete Event Simulation Library
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 
276 // The debugger needs to check if the exception that is being thrown is a termination exception or not,
277 // because the user mostly doesn't want to break into the debugger when the simulation terminates.
278 // Unfortunately, the std::type_info structure can't easily be used in the breakpoint conditional expression,
279 // so we provide this function that returns a pointer to the type_info of cTerminationException.
280 extern const std::type_info *getTerminationExceptionTypeInfoPointer();
281 
292 class SIM_API cRuntimeError : public cException
293 {
294  public:
295  // internal
296  bool displayed = false;
297  protected:
298  // internal
299  void notifyEnvir();
300 
301  public:
302 
308  cRuntimeError(ErrorCodeInt errcode,...);
309 
313  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
314  cRuntimeError(const char *msg,...);
315 
323  cRuntimeError(const cObject *where, ErrorCodeInt errcode,...);
324 
330  _OPP_GNU_ATTRIBUTE(format(printf, 3, 4))
331  cRuntimeError(const cObject *where, const char *msg,...);
332 
336  cRuntimeError(const std::exception& e, const char *location);
337 
343 
348  virtual cRuntimeError *dup() const override {return new cRuntimeError(*this);}
349 };
350 
357 class SIM_API cDeleteModuleException : public cException
358 {
359  protected:
360  cModule *toDelete;
361 
362  public:
366  cDeleteModuleException(cModule *toDelete) : cException(), toDelete(toDelete) {}
367 
373 
378  virtual cDeleteModuleException *dup() const override {return new cDeleteModuleException(*this);}
379 
383  virtual cModule *getModuleToDelete() const {return toDelete;}
384 
388  virtual bool isError() const override {return false;}
389 };
390 
400 class SIM_API cStackCleanupException : public cException
401 {
402  public:
407 
413 
418  virtual cStackCleanupException *dup() const override {return new cStackCleanupException(*this);}
419 
423  virtual bool isError() const override {return false;}
424 };
425 
426 } // namespace omnetpp
427 
428 
429 #endif
430 
431 
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:388
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:342
omnetpp::cDeleteModuleException::getModuleToDelete
virtual cModule * getModuleToDelete() const
Definition: cexception.h:383
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:348
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:400
omnetpp::cStackCleanupException::cStackCleanupException
cStackCleanupException()
Definition: cexception.h:406
omnetpp::cDeleteModuleException
Thrown from deleteModule() when the active activity() module is about to be deleted,...
Definition: cexception.h:357
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:418
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:378
omnetpp::cException::getContextFullPath
virtual const char * getContextFullPath() const
Definition: cexception.h:213
omnetpp::cStackCleanupException::isError
virtual bool isError() const override
Definition: cexception.h:423
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:292
omnetpp::cException::what
virtual const char * what() const override
Definition: cexception.h:169
omnetpp::cDeleteModuleException::cDeleteModuleException
cDeleteModuleException(cModule *toDelete)
Definition: cexception.h:366
omnetpp::cException::getSimulationStage
virtual int getSimulationStage() const
Definition: cexception.h:184