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__, std::move(simgrid::xbt::Backtrace()), xbt_procname(), \
60 /** Ancestor class of all SimGrid exception */
61 class Exception : public std::runtime_error {
63 Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
64 : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
68 /** Return the information about where the exception was thrown */
69 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
71 std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
74 xbt::ThrowPoint throwpoint_;
77 } // namespace simgrid
79 /** A legacy exception
81 * It is defined by a category and a value within that category (as well as
82 * an optional error message).
84 * This used to be a structure for C exceptions but it has been retrofitted
85 * as a C++ exception and some of its data has been moved in the
86 * @ref WithContextException base class. We should deprecate it and replace it
87 * with either C++ different exceptions or `std::system_error` which already
88 * provides this (category + error code) logic.
92 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
96 * @param throwpoint Throw point (use XBT_THROW_POINT)
97 * @param message Exception message
99 xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message)
100 : simgrid::Exception(std::move(throwpoint), std::move(message))
104 xbt_ex(const xbt_ex&) = default;
106 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
108 /** Category (what went wrong) */
109 xbt_errcat_t category = unknown_error;
111 /** Why did it went wrong */
117 /** Exception raised when a timeout elapsed */
118 class TimeoutError : public xbt_ex {
120 TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message)
121 : xbt_ex(std::move(throwpoint), std::move(message))
123 category = timeout_error;
127 /** Exception raised when a host fails */
128 class HostFailureException : public xbt_ex {
130 HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
131 : xbt_ex(std::move(throwpoint), std::move(message))
133 category = host_error;
137 /** Exception raised when a communication fails because of the network */
138 class NetworkFailureException : public xbt_ex {
140 NetworkFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
141 : xbt_ex(std::move(throwpoint), std::move(message))
143 category = network_error;
147 /** Exception raised when something got canceled before completion */
148 class CancelException : public xbt_ex {
150 CancelException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
151 : xbt_ex(std::move(throwpoint), std::move(message))
153 category = cancel_error;
157 } // namespace simgrid