cexception.h Source File¶

API: cexception.h Source File
API
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 "simutil.h"
24#include "simtime_t.h"
25#include "errmsg.h"
26
27#if __cplusplus >= 202002L // only with C++20 or later
28#include <format>
29#endif
30
31namespace omnetpp {
32
33class cObject;
34class cComponent;
35class cModule;
36
47typedef int ErrorCodeInt;
48
54class SIM_API cException : public std::exception
55{
56 protected:
57 ErrorCode errorCode;
58 std::string msg;
59
60 int simulationStage;
61 eventnumber_t eventNumber;
62 simtime_t simtime;
63
64 bool hasContext_;
65 std::string contextClassName;
66 std::string contextFullPath;
67 int contextComponentId;
68 int contextComponentKind; // actually cComponent::ComponentKind
69
75 void init(const cObject *obj, ErrorCode errorcode, const std::string& msg);
76
77 // helper for init()
78 void storeContext();
79
80 // default constructor, for subclasses only.
81 cException();
82
83 //
84 // Helper, called from cException constructors.
85 //
86 // If an exception occurs in initialization code (during construction of
87 // global objects, before main() is called), there is nobody who could
88 // catch the error, so it would just cause a program abort.
89 // Here we handle this case manually: if cException ctor is invoked before
90 // main() has started, we print the error message and call exit(1).
91 //
92 void exitIfStartupError();
93
94 public:
97
102 cException(ErrorCodeInt errcode,...);
103
107 _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
108 cException(const char *msg,...);
109
117 cException(const cObject *where, ErrorCodeInt errcode,...);
118
124 _OPP_GNU_ATTRIBUTE(format(printf, 3, 4))
125 cException(const cObject *where, const char *msg,...);
126
127#if __cplusplus >= 202002L // only with C++20 or later
128
134 template<typename... Args>
135 cException(opp_fmtstr fmt, Args&&... args) {
136 init(nullptr, E_CUSTOM, std::vformat(fmt.str, std::make_format_args(args...)));
137 }
138
144 template<typename... Args>
145 cException(const cObject *where, opp_fmtstr fmt, Args&&... args) {
146 init(where, E_CUSTOM, std::vformat(fmt.str, std::make_format_args(args...)));
147 }
148
149#endif // C++20
150
155 cException(const cException&) = default;
156
161 virtual cException *dup() const {return new cException(*this);}
162
166 virtual ~cException() throw() {}
168
171
174 virtual void setMessage(const char *txt) {msg = txt;}
175
179 _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
180 virtual void prependMessage(const char *fmt, ...);
181
185 _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
186 virtual void appendMessage(const char *fmt, ...);
188
191
195 virtual bool isError() const {return true;}
196
200 virtual int getErrorCode() const {return errorCode;}
201
205 virtual const char *what() const throw() override {return msg.c_str();}
206
212 virtual std::string getFormattedMessage() const;
213
220 virtual int getSimulationStage() const {return simulationStage;}
221
225 virtual eventnumber_t getEventNumber() const {return eventNumber;}
226
230 virtual simtime_t getSimtime() const {return simtime;}
231
237 virtual bool hasContext() const {return hasContext_;}
238
243 virtual const char *getContextClassName() const {return contextClassName.c_str();}
244
249 virtual const char *getContextFullPath() const {return contextFullPath.c_str();}
250
257 virtual int getContextComponentId() const {return contextComponentId;}
258
263 virtual int getContextComponentKind() const {return contextComponentKind;}
265};
266
277class SIM_API cTerminationException : public cException
278{
279 public:
285 cTerminationException(ErrorCodeInt errcode,...);
286
290 _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
291 cTerminationException(const char *msg,...);
292
293#if __cplusplus >= 202002L // only with C++20 or later
294
300 template<typename... Args>
301 cTerminationException(opp_fmtstr fmt, Args&&... args) {
302 init(nullptr, E_CUSTOM, std::vformat(fmt.str, std::make_format_args(args...)));
303 }
304
305#endif
306
312
317 virtual cTerminationException *dup() const override {return new cTerminationException(*this);}
318
323 virtual bool isError() const override {return false;}
324};
325
326// The debugger needs to check if the exception that is being thrown is a termination exception or not,
327// because the user mostly doesn't want to break into the debugger when the simulation terminates.
328// Unfortunately, the std::type_info structure can't easily be used in the breakpoint conditional expression,
329// so we provide this function that returns a pointer to the type_info of cTerminationException.
330extern const std::type_info *getTerminationExceptionTypeInfoPointer();
331
342class SIM_API cRuntimeError : public cException
343{
344 public:
345 // internal
346 bool displayed = false;
347 protected:
348 // internal
349 void notifyEnvir();
350
351 public:
352
358 cRuntimeError(ErrorCodeInt errcode,...);
359
363 _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
364 cRuntimeError(const char *msg,...);
365
373 cRuntimeError(const cObject *where, ErrorCodeInt errcode,...);
374
380 _OPP_GNU_ATTRIBUTE(format(printf, 3, 4))
381 cRuntimeError(const cObject *where, const char *msg,...);
382
383#if __cplusplus >= 202002L // only with C++20 or later
384
390 template<typename... Args>
391 cRuntimeError(opp_fmtstr fmt, Args&&... args) {
392 init(nullptr, E_CUSTOM, std::vformat(fmt.str, std::make_format_args(args...)));
393 notifyEnvir();
394 }
395
401 template<typename... Args>
402 cRuntimeError(const cObject *where, opp_fmtstr fmt, Args&&... args) {
403 init(where, E_CUSTOM, std::vformat(fmt.str, std::make_format_args(args...)));
404 notifyEnvir();
405 }
406
407#endif
408
412 cRuntimeError(const std::exception& e, const char *location);
413
418 cRuntimeError(const cRuntimeError& e) : cException(e) {}
419
424 virtual cRuntimeError *dup() const override {return new cRuntimeError(*this);}
425};
426
433class SIM_API cDeleteModuleException : public cException
434{
435 protected:
436 cModule *toDelete;
437
438 public:
442 cDeleteModuleException(cModule *toDelete) : cException(), toDelete(toDelete) {}
443
449
454 virtual cDeleteModuleException *dup() const override {return new cDeleteModuleException(*this);}
455
459 virtual cModule *getModuleToDelete() const {return toDelete;}
460
464 virtual bool isError() const override {return false;}
465};
466
476class SIM_API cStackCleanupException : public cException
477{
478 public:
482 cStackCleanupException() : cException() {}
483
489
494 virtual cStackCleanupException *dup() const override {return new cStackCleanupException(*this);}
495
499 virtual bool isError() const override {return false;}
500};
501
502} // namespace omnetpp
503
504
505#endif
506
507
Common base for module and channel classes.
Definition ccomponent.h:51
cDeleteModuleException(cModule *toDelete)
Definition cexception.h:442
virtual cDeleteModuleException * dup() const override
Definition cexception.h:454
virtual cModule * getModuleToDelete() const
Definition cexception.h:459
virtual bool isError() const override
Definition cexception.h:464
cDeleteModuleException(const cDeleteModuleException &e)=default
Exception class.
Definition cexception.h:55
virtual bool isError() const
Definition cexception.h:195
virtual const char * what() const override
Definition cexception.h:205
virtual bool hasContext() const
Definition cexception.h:237
cException(ErrorCodeInt errcode,...)
virtual void setMessage(const char *txt)
Definition cexception.h:174
virtual int getSimulationStage() const
Definition cexception.h:220
virtual ~cException()
Definition cexception.h:166
virtual cException * dup() const
Definition cexception.h:161
virtual const char * getContextClassName() const
Definition cexception.h:243
virtual void prependMessage(const char *fmt,...)
cException(const cException &)=default
virtual int getErrorCode() const
Definition cexception.h:200
virtual void appendMessage(const char *fmt,...)
virtual int getContextComponentKind() const
Definition cexception.h:263
virtual std::string getFormattedMessage() const
virtual eventnumber_t getEventNumber() const
Definition cexception.h:225
virtual const char * getContextFullPath() const
Definition cexception.h:249
void init(const cObject *obj, ErrorCode errorcode, const std::string &msg)
virtual int getContextComponentId() const
Definition cexception.h:257
virtual simtime_t getSimtime() const
Definition cexception.h:230
This class represents modules in the simulation.
Definition cmodule.h:49
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition cobject.h:93
Thrown when the simulation kernel or other components detect a runtime error.
Definition cexception.h:343
cRuntimeError(const std::exception &e, const char *location)
virtual cRuntimeError * dup() const override
Definition cexception.h:424
cRuntimeError(ErrorCodeInt errcode,...)
cRuntimeError(const cRuntimeError &e)
Definition cexception.h:418
virtual cStackCleanupException * dup() const override
Definition cexception.h:494
cStackCleanupException(const cStackCleanupException &e)=default
cStackCleanupException()
Definition cexception.h:482
virtual bool isError() const override
Definition cexception.h:499
virtual cTerminationException * dup() const override
Definition cexception.h:317
cTerminationException(ErrorCodeInt errcode,...)
cTerminationException(const cTerminationException &e)=default
virtual bool isError() const override
Definition cexception.h:323
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition simkerneldefs.h:78
SimTime simtime_t
Represents simulation time.
Definition simtime_t.h:40