1 /* Copyright (c) 2018. 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.
17 #include <type_traits>
20 #include <xbt/backtrace.h>
21 #include <xbt/backtrace.hpp>
25 #include <xbt/misc.h> // xbt_procname
26 #include <xbt/virtu.h> // xbt_getpid
31 /** The location of where an exception has been thrown
33 * This is a tuple (__FILE__, __LINE__, __func__), created with @ref XBT_THROW_POINT.
39 ThrowPoint() = default;
40 explicit ThrowPoint(const char* file, int line, const char* function) : file_(file), line_(line), function_(function)
43 const char* file_ = nullptr;
45 const char* function_ = nullptr;
48 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
49 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
51 /** A base class for exceptions with context
53 * This is a base class for exceptions which store additional contextual
54 * information: backtrace, throw point, simulated process name, PID, etc.
56 class XBT_PUBLIC ContextedException {
58 ContextedException() : backtrace_(simgrid::xbt::backtrace()), procname_(xbt_procname()), pid_(xbt_getpid()) {}
59 explicit ContextedException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
62 explicit ContextedException(ThrowPoint throwpoint, Backtrace bt)
63 : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
66 virtual ~ContextedException();
67 Backtrace const& backtrace() const { return backtrace_; }
68 int pid() const { return pid_; }
69 std::string const& process_name() const { return procname_; }
70 ThrowPoint& throw_point() { return throwpoint_; }
73 XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
75 return process_name();
77 XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
84 std::string procname_; /**< Name of the process who thrown this */
85 int pid_; /**< PID of the process who thrown this */
86 ThrowPoint throwpoint_;
90 /** Ancestor class of all SimGrid exception */
91 class Exception : public std::runtime_error {
93 Exception() : std::runtime_error("") {}
94 Exception(const char* message) : std::runtime_error(message) {}
97 /** Exception raised when a timeout elapsed */
98 class timeout_error : public simgrid::Exception {
101 /** Exception raised when an host fails */
102 class host_failure : public simgrid::Exception {
105 /** Exception raised when a communication fails because of the network */
106 class network_failure : public simgrid::Exception {
109 /** Exception raised when something got canceled before completion */
110 class canceled_error : public simgrid::Exception {
113 } // namespace simgrid
115 /** A legacy exception
117 * It is defined by a category and a value within that category (as well as
118 * an optional error message).
120 * This used to be a structure for C exceptions but it has been retrofitted
121 * as a C++ exception and some of its data has been moved in the
122 * @ref WithContextException base class. We should deprecate it and replace it
123 * with either C++ different exceptions or `std::system_error` which already
124 * provides this (category + error code) logic.
128 class XBT_PUBLIC xbt_ex : public simgrid::Exception, public simgrid::xbt::ContextedException {
130 xbt_ex() : simgrid::Exception() {}
134 * @param throwpoint Throw point (use XBT_THROW_POINT)
135 * @param message Exception message
137 xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message)
138 : simgrid::Exception(message), simgrid::xbt::ContextedException(throwpoint, simgrid::xbt::backtrace())
142 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
144 /** Category (what went wrong) */
145 xbt_errcat_t category = unknown_error;
147 /** Why did it went wrong */