Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Declare move constructors with "noexcept".
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_EXCEPTIONS_HPP
7 #define SIMGRID_EXCEPTIONS_HPP
8
9 /** @file exception.hpp SimGrid-specific Exceptions
10  *
11  *  Defines all possible exception that could occur in a SimGrid library.
12  */
13
14 #include <xbt/backtrace.hpp>
15 #include <xbt/ex.h>
16 #include <xbt/string.hpp>
17
18 #include <atomic>
19 #include <functional>
20 #include <stdexcept>
21 #include <string>
22
23 namespace simgrid {
24 namespace xbt {
25
26 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
27  *
28  *  Constitute the contextual information of where an exception was thrown
29  *
30  *  These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
31  *  are best created with @ref XBT_THROW_POINT.
32  *
33  *  @ingroup XBT_ex
34  */
35 class ThrowPoint {
36 public:
37   ThrowPoint() = default;
38   explicit ThrowPoint(const char* file, int line, const char* function, Backtrace&& bt, std::string&& actor_name,
39                       int pid)
40       : file_(file)
41       , line_(line)
42       , function_(function)
43       , backtrace_(std::move(bt))
44       , procname_(std::move(actor_name))
45       , pid_(pid)
46   {
47   }
48
49   const char* file_     = nullptr;
50   int line_             = 0;
51   const char* function_ = nullptr;
52   Backtrace backtrace_;
53   std::string procname_ = ""; /**< Name of the process who thrown this */
54   int pid_              = 0;  /**< PID of the process who thrown this */
55 };
56
57 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
58 #define XBT_THROW_POINT                                                                                                \
59   ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid())
60
61 class XBT_PUBLIC ImpossibleError : public std::logic_error {
62 public:
63   explicit ImpossibleError(const std::string& arg) : std::logic_error(arg) {}
64   ~ImpossibleError();
65 };
66
67 class XBT_PUBLIC InitializationError : public std::logic_error {
68 public:
69   explicit InitializationError(const std::string& arg) : std::logic_error(arg) {}
70   ~InitializationError();
71 };
72
73 class XBT_PUBLIC UnimplementedError : public std::logic_error {
74 public:
75   explicit UnimplementedError(const std::string& arg) : std::logic_error(arg) {}
76   ~UnimplementedError();
77 };
78
79 } // namespace xbt
80
81 /** Ancestor class of all SimGrid exception */
82 class Exception : public std::runtime_error {
83 public:
84   Exception(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
85       : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
86   {
87   }
88   Exception(const Exception&)     = default;
89   Exception(Exception&&) noexcept = default;
90   ~Exception(); // DO NOT define it here -- see Exception.cpp for a rationale
91
92   /** Return the information about where the exception was thrown */
93   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
94
95   std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
96
97   /** Allow to carry a value (used by waitall/waitany) */
98   int value = 0;
99
100 private:
101   xbt::ThrowPoint throwpoint_;
102 };
103
104 /** Exception raised when a timeout elapsed */
105 class TimeoutException : public Exception {
106 public:
107   TimeoutException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
108       : Exception(std::move(throwpoint), std::move(message))
109   {
110   }
111   TimeoutException(const TimeoutException&)     = default;
112   TimeoutException(TimeoutException&&) noexcept = default;
113 };
114
115 XBT_ATTRIB_DEPRECATED_v328("Please use simgrid::TimeoutException") typedef TimeoutException TimeoutError;
116
117 /** Exception raised when a host fails */
118 class HostFailureException : public Exception {
119 public:
120   HostFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
121       : Exception(std::move(throwpoint), std::move(message))
122   {
123   }
124   HostFailureException(const HostFailureException&)     = default;
125   HostFailureException(HostFailureException&&) noexcept = default;
126 };
127
128 /** Exception raised when a communication fails because of the network or because of the remote host */
129 class NetworkFailureException : public Exception {
130 public:
131   NetworkFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
132       : Exception(std::move(throwpoint), std::move(message))
133   {
134   }
135   NetworkFailureException(const NetworkFailureException&)     = default;
136   NetworkFailureException(NetworkFailureException&&) noexcept = default;
137 };
138
139 /** Exception raised when a storage fails */
140 class StorageFailureException : public Exception {
141 public:
142   StorageFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
143       : Exception(std::move(throwpoint), std::move(message))
144   {
145   }
146   StorageFailureException(const StorageFailureException&)     = default;
147   StorageFailureException(StorageFailureException&&) noexcept = default;
148 };
149
150 /** Exception raised when a VM fails */
151 class VmFailureException : public Exception {
152 public:
153   VmFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
154       : Exception(std::move(throwpoint), std::move(message))
155   {
156   }
157   VmFailureException(const VmFailureException&)     = default;
158   VmFailureException(VmFailureException&&) noexcept = default;
159 };
160
161 /** Exception raised when something got canceled before completion */
162 class CancelException : public Exception {
163 public:
164   CancelException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
165       : Exception(std::move(throwpoint), std::move(message))
166   {
167   }
168   CancelException(const CancelException&)     = default;
169   CancelException(CancelException&&) noexcept = default;
170 };
171
172 /** Exception raised when something is going wrong during the simulation tracing */
173 class TracingError : public Exception {
174 public:
175   TracingError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
176       : Exception(std::move(throwpoint), std::move(message))
177   {
178   }
179   TracingError(const TracingError&)     = default;
180   TracingError(TracingError&&) noexcept = default;
181 };
182
183 /** Exception raised when something is going wrong during the parsing of XML files */
184 class ParseError : public Exception {
185 public:
186   ParseError(const std::string& file, int line, const std::string& msg)
187       : Exception(XBT_THROW_POINT, xbt::string_printf("Parse error at %s:%d: %s", file.c_str(), line, msg.c_str()))
188   {
189   }
190   ParseError(const ParseError&)     = default;
191   ParseError(ParseError&&) noexcept = default;
192 };
193
194 class XBT_PUBLIC ForcefulKillException {
195   /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
196    *
197    * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
198    * all objects allocated on the stack (RAII powa).
199    *
200    * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
201    * SURVIVE a ForcefulKillException, or your simulation will segfault.
202    *
203    * @verbatim
204    * void* payload = malloc(512);
205    *
206    * try {
207    *   simgrid::s4u::this_actor::execute(100000);
208    * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
209    *   free(malloc);
210    *   throw; // I shall never survive on an host that was switched off
211    * }
212    * @endverbatim
213    */
214   /* Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
215    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
216    */
217 public:
218   ForcefulKillException() = default;
219   explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
220   {
221   }
222   ~ForcefulKillException();
223   const char* what() const noexcept { return msg_.c_str(); }
224
225   XBT_ATTRIB_NORETURN static void do_throw();
226   static bool try_n_catch(const std::function<void()>& try_block);
227
228 private:
229   std::string msg_ = std::string("Actor killed.");
230 };
231
232 } // namespace simgrid
233
234 XBT_ATTRIB_DEPRECATED_v328("Please use simgrid::Exception") typedef simgrid::Exception xbt_ex;
235
236 #endif