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>
24 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
26 * Constitute the contextual information of where an exception was thrown
28 * These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
29 * are best created with @ref XBT_THROW_POINT.
35 ThrowPoint() = default;
36 explicit ThrowPoint(const char* file, int line, const char* function, Backtrace bt, std::string actor_name, int pid)
40 , backtrace_(std::move(bt))
41 , procname_(std::move(actor_name))
46 const char* file_ = nullptr;
48 const char* function_ = nullptr;
50 std::string procname_ = ""; /**< Name of the process who thrown this */
51 int pid_ = 0; /**< PID of the process who thrown this */
54 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
55 #define XBT_THROW_POINT \
56 ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid())
59 /** Ancestor class of all SimGrid exception */
60 class Exception : public std::runtime_error {
62 Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
63 : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
67 /** Return the information about where the exception was thrown */
68 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
70 std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
73 xbt::ThrowPoint throwpoint_;
76 } // namespace simgrid
78 /** A legacy exception
80 * It is defined by a category and a value within that category (as well as
81 * an optional error message).
83 * This used to be a structure for C exceptions but it has been retrofitted
84 * as a C++ exception and some of its data has been moved in the
85 * @ref WithContextException base class. We should deprecate it and replace it
86 * with either C++ different exceptions or `std::system_error` which already
87 * provides this (category + error code) logic.
91 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
95 * @param throwpoint Throw point (use XBT_THROW_POINT)
96 * @param message Exception message
98 xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message)
99 : simgrid::Exception(std::move(throwpoint), std::move(message))
103 xbt_ex(const xbt_ex&) = default;
105 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
107 /** Category (what went wrong) */
108 xbt_errcat_t category = unknown_error;
110 /** Why did it went wrong */
116 /** Exception raised when a timeout elapsed */
117 class TimeoutError : public xbt_ex {
119 TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message)
120 : xbt_ex(std::move(throwpoint), std::move(message))
122 category = timeout_error;
126 /** Exception raised when a host fails */
127 class HostFailureException : public xbt_ex {
129 HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
130 : xbt_ex(std::move(throwpoint), std::move(message))
132 category = host_error;
136 /** Exception raised when a communication fails because of the network or because of the remote host */
137 class NetworkFailureException : public xbt_ex {
139 NetworkFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
140 : xbt_ex(std::move(throwpoint), std::move(message))
142 category = network_error;
146 /** Exception raised when a storage fails */
147 class StorageFailureException : public xbt_ex {
149 StorageFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
150 : xbt_ex(std::move(throwpoint), std::move(message))
156 /** Exception raised when something got canceled before completion */
157 class CancelException : public xbt_ex {
159 CancelException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
160 : xbt_ex(std::move(throwpoint), std::move(message))
162 category = cancel_error;
166 } // namespace simgrid