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 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
33 * Constitute the contextual information of where an exception was thrown
35 * These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
36 * are best created with @ref XBT_THROW_POINT.
42 ThrowPoint() = default;
43 explicit ThrowPoint(const char* file, int line, const char* function, Backtrace bt, std::string actor_name, int pid)
44 : file_(file), line_(line), function_(function), backtrace_(bt), procname_(actor_name), pid_(pid)
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())
61 /** Ancestor class of all SimGrid exception */
62 class Exception : public std::runtime_error {
64 Exception(simgrid::xbt::ThrowPoint throwpoint, const char* message)
65 : std::runtime_error(message), throwpoint_(throwpoint)
69 /** Return the information about where the exception was thrown */
70 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
73 xbt::ThrowPoint throwpoint_;
76 /** Exception raised when a timeout elapsed */
77 class timeout_error : public simgrid::Exception {
80 /** Exception raised when an host fails */
81 class host_failure : public simgrid::Exception {
84 /** Exception raised when a communication fails because of the network */
85 class network_failure : public simgrid::Exception {
88 /** Exception raised when something got canceled before completion */
89 class canceled_error : public simgrid::Exception {
92 } // namespace simgrid
94 /** A legacy exception
96 * It is defined by a category and a value within that category (as well as
97 * an optional error message).
99 * This used to be a structure for C exceptions but it has been retrofitted
100 * as a C++ exception and some of its data has been moved in the
101 * @ref WithContextException base class. We should deprecate it and replace it
102 * with either C++ different exceptions or `std::system_error` which already
103 * 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, const char* message) : simgrid::Exception(throwpoint, message) {}
116 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
118 /** Category (what went wrong) */
119 xbt_errcat_t category = unknown_error;
121 /** Why did it went wrong */