1 /* Copyright (c) 2018-2019. 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>
25 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
27 * Constitute the contextual information of where an exception was thrown
29 * These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
30 * are best created with @ref XBT_THROW_POINT.
36 ThrowPoint() = default;
37 explicit ThrowPoint(const char* file, int line, const char* function, Backtrace&& bt, std::string&& actor_name,
42 , backtrace_(std::move(bt))
43 , procname_(std::move(actor_name))
48 const char* file_ = nullptr;
50 const char* function_ = nullptr;
52 std::string procname_ = ""; /**< Name of the process who thrown this */
53 int pid_ = 0; /**< PID of the process who thrown this */
56 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
57 #define XBT_THROW_POINT \
58 ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid())
60 class XBT_PUBLIC ImpossibleError : public std::logic_error {
62 explicit ImpossibleError(const std::string& arg) : std::logic_error(arg) {}
66 class XBT_PUBLIC UnimplementedError : public std::logic_error {
68 explicit UnimplementedError(const std::string& arg) : std::logic_error(arg) {}
69 ~UnimplementedError();
74 /** Ancestor class of all SimGrid exception */
75 class Exception : public std::runtime_error {
77 Exception(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
78 : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
82 /** Return the information about where the exception was thrown */
83 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
85 std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
88 xbt::ThrowPoint throwpoint_;
91 } // namespace simgrid
93 /** A legacy exception
95 * It is defined by a category and a value within that category (as well as
96 * an optional error message).
98 * This used to be a structure for C exceptions but it has been retrofitted
99 * as a C++ exception and some of its data has been moved in the
100 * @ref WithContextException base class. We should deprecate it and replace it
101 * with either C++ different exceptions or `std::system_error` which already
102 * provides this (category + error code) logic.
107 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
111 * @param throwpoint Throw point (use XBT_THROW_POINT)
112 * @param message Exception message
114 xbt_ex(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
115 : simgrid::Exception(std::move(throwpoint), std::move(message))
119 xbt_ex(const xbt_ex&) = default;
121 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
123 /** Category (what went wrong) */
124 xbt_errcat_t category = unknown_error;
126 /** Why did it went wrong */
132 /** Exception raised when a timeout elapsed */
133 class TimeoutError : public xbt_ex {
135 TimeoutError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
136 : xbt_ex(std::move(throwpoint), std::move(message))
138 category = timeout_error;
142 /** Exception raised when a host fails */
143 class HostFailureException : public xbt_ex {
145 HostFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
146 : xbt_ex(std::move(throwpoint), std::move(message))
148 category = host_error;
152 /** Exception raised when a communication fails because of the network or because of the remote host */
153 class NetworkFailureException : public xbt_ex {
155 NetworkFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
156 : xbt_ex(std::move(throwpoint), std::move(message))
158 category = network_error;
162 /** Exception raised when a storage fails */
163 class StorageFailureException : public xbt_ex {
165 StorageFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
166 : xbt_ex(std::move(throwpoint), std::move(message))
172 /** Exception raised when a VM fails */
173 class VmFailureException : public xbt_ex {
175 VmFailureException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
176 : xbt_ex(std::move(throwpoint), std::move(message))
182 /** Exception raised when something got canceled before completion */
183 class CancelException : public xbt_ex {
185 CancelException(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
186 : xbt_ex(std::move(throwpoint), std::move(message))
188 category = cancel_error;
192 /** Exception raised when something is going wrong during the simulation tracing */
193 class TracingError : public xbt_ex {
195 TracingError(simgrid::xbt::ThrowPoint&& throwpoint, std::string&& message)
196 : xbt_ex(std::move(throwpoint), std::move(message))
198 category = tracing_error;
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 an 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