1 /* Copyright (c) 2018-2020. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_EXCEPTIONS_HPP
7 #define SIMGRID_EXCEPTIONS_HPP
9 /** @file exception.hpp SimGrid-specific Exceptions
11 * Defines all possible exception that could occur in a SimGrid library.
14 #include <xbt/backtrace.hpp>
16 #include <xbt/string.hpp>
26 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
28 * Constitute the contextual information of where an exception was thrown
30 * These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
31 * are best created with @ref XBT_THROW_POINT.
37 ThrowPoint() = default;
38 explicit ThrowPoint(const char* file, int line, const char* function, Backtrace&& bt, std::string&& actor_name,
43 , backtrace_(std::move(bt))
44 , procname_(std::move(actor_name))
49 const char* file_ = nullptr;
51 const char* function_ = nullptr;
53 std::string procname_ = ""; /**< Name of the process who thrown this */
54 int pid_ = 0; /**< PID of the process who thrown this */
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())
61 class XBT_PUBLIC ImpossibleError : public std::logic_error {
63 explicit ImpossibleError(const std::string& arg) : std::logic_error(arg) {}
64 ~ImpossibleError() override;
67 class XBT_PUBLIC InitializationError : public std::logic_error {
69 explicit InitializationError(const std::string& arg) : std::logic_error(arg) {}
70 ~InitializationError() override;
73 class XBT_PUBLIC UnimplementedError : public std::logic_error {
75 explicit UnimplementedError(const std::string& arg) : std::logic_error(arg) {}
76 ~UnimplementedError() override;
81 /** Ancestor class of all SimGrid exception */
82 class Exception : public std::runtime_error {
84 Exception(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
85 : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
88 Exception(const Exception&) = default;
89 Exception(Exception&&) noexcept = default;
90 ~Exception() override; // DO NOT define it here -- see Exception.cpp for a rationale
92 /** Return the information about where the exception was thrown */
93 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
95 std::string resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
97 /** Allow to carry a value (used by waitall/waitany) */
101 xbt::ThrowPoint throwpoint_;
104 /** Exception raised when a timeout elapsed */
105 class TimeoutException : public Exception {
107 TimeoutException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
108 : Exception(std::move(throwpoint), std::move(message))
111 TimeoutException(const TimeoutException&) = default;
112 TimeoutException(TimeoutException&&) noexcept = default;
113 ~TimeoutException() override;
116 using TimeoutError XBT_ATTRIB_DEPRECATED_v328("Please use simgrid::TimeoutException") = TimeoutException;
118 /** Exception raised when a host fails */
119 class HostFailureException : public Exception {
121 HostFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
122 : Exception(std::move(throwpoint), std::move(message))
125 HostFailureException(const HostFailureException&) = default;
126 HostFailureException(HostFailureException&&) noexcept = default;
127 ~HostFailureException() override;
130 /** Exception raised when a communication fails because of the network or because of the remote host */
131 class NetworkFailureException : public Exception {
133 NetworkFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
134 : Exception(std::move(throwpoint), std::move(message))
137 NetworkFailureException(const NetworkFailureException&) = default;
138 NetworkFailureException(NetworkFailureException&&) noexcept = default;
139 ~NetworkFailureException() override;
142 /** Exception raised when a storage fails */
143 class StorageFailureException : public Exception {
145 StorageFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
146 : Exception(std::move(throwpoint), std::move(message))
149 StorageFailureException(const StorageFailureException&) = default;
150 StorageFailureException(StorageFailureException&&) noexcept = default;
151 ~StorageFailureException() override;
154 /** Exception raised when a VM fails */
155 class VmFailureException : public Exception {
157 VmFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
158 : Exception(std::move(throwpoint), std::move(message))
161 VmFailureException(const VmFailureException&) = default;
162 VmFailureException(VmFailureException&&) noexcept = default;
163 ~VmFailureException() override;
166 /** Exception raised when something got canceled before completion */
167 class CancelException : public Exception {
169 CancelException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
170 : Exception(std::move(throwpoint), std::move(message))
173 CancelException(const CancelException&) = default;
174 CancelException(CancelException&&) noexcept = default;
175 ~CancelException() override;
178 /** Exception raised when something is going wrong during the simulation tracing */
179 class TracingError : public Exception {
181 TracingError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
182 : Exception(std::move(throwpoint), std::move(message))
185 TracingError(const TracingError&) = default;
186 TracingError(TracingError&&) noexcept = default;
187 ~TracingError() override;
190 /** Exception raised when something is going wrong during the parsing of XML files */
191 class ParseError : public Exception {
193 ParseError(const std::string& file, int line, const std::string& msg)
194 : Exception(XBT_THROW_POINT, xbt::string_printf("Parse error at %s:%d: %s", file.c_str(), line, msg.c_str()))
197 ParseError(const ParseError&) = default;
198 ParseError(ParseError&&) noexcept = default;
199 ~ParseError() override;
202 class XBT_PUBLIC ForcefulKillException {
203 /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
205 * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
206 * all objects allocated on the stack (RAII powa).
208 * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
209 * SURVIVE a ForcefulKillException, or your simulation will segfault.
212 * void* payload = malloc(512);
215 * simgrid::s4u::this_actor::execute(100000);
216 * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
218 * throw; // I shall never survive on a host that was switched off
222 /* Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
223 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
226 ForcefulKillException() = default;
227 explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
230 ~ForcefulKillException();
231 const char* what() const noexcept { return msg_.c_str(); }
233 XBT_ATTRIB_NORETURN static void do_throw();
234 static bool try_n_catch(const std::function<void()>& try_block);
237 std::string msg_ = std::string("Actor killed.");
240 } // namespace simgrid
242 using xbt_ex XBT_ATTRIB_DEPRECATED_v328("Please use simgrid::Exception") = simgrid::Exception;