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, std::string 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 } // 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) : simgrid::Exception(throwpoint, message) {}
100 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
102 /** Category (what went wrong) */
103 xbt_errcat_t category = unknown_error;
105 /** Why did it went wrong */
111 /** Exception raised when a timeout elapsed */
112 class TimeoutError : public xbt_ex {
114 TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
116 category = timeout_error;
120 /** Exception raised when an host fails */
121 class HostFailureException : public xbt_ex {
123 HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
125 category = host_error;
129 /** Exception raised when a communication fails because of the network */
130 class NetworkFailureException : public xbt_ex {
133 /** Exception raised when something got canceled before completion */
134 class CancelException : public xbt_ex {
136 } // namespace simgrid