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.
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)
37 : file_(file), line_(line), function_(function), backtrace_(std::move(bt)), procname_(actor_name), pid_(pid)
41 const char* file_ = nullptr;
43 const char* function_ = nullptr;
45 std::string procname_ = ""; /**< Name of the process who thrown this */
46 int pid_ = 0; /**< PID of the process who thrown this */
49 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
50 #define XBT_THROW_POINT \
51 ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, std::move(simgrid::xbt::Backtrace()), xbt_procname(), \
55 /** Ancestor class of all SimGrid exception */
56 class Exception : public std::runtime_error {
58 Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
59 : std::runtime_error(message), throwpoint_(throwpoint)
63 /** Return the information about where the exception was thrown */
64 xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
67 xbt::ThrowPoint throwpoint_;
70 } // namespace simgrid
72 /** A legacy exception
74 * It is defined by a category and a value within that category (as well as
75 * an optional error message).
77 * This used to be a structure for C exceptions but it has been retrofitted
78 * as a C++ exception and some of its data has been moved in the
79 * @ref WithContextException base class. We should deprecate it and replace it
80 * with either C++ different exceptions or `std::system_error` which already
81 * provides this (category + error code) logic.
85 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
89 * @param throwpoint Throw point (use XBT_THROW_POINT)
90 * @param message Exception message
92 xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message) : simgrid::Exception(throwpoint, message) {}
94 ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
96 /** Category (what went wrong) */
97 xbt_errcat_t category = unknown_error;
99 /** Why did it went wrong */
105 /** Exception raised when a timeout elapsed */
106 class TimeoutError : public xbt_ex {
108 TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
110 category = timeout_error;
114 /** Exception raised when an host fails */
115 class HostFailureException : public xbt_ex {
117 HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
119 category = host_error;
123 /** Exception raised when a communication fails because of the network */
124 class NetworkFailureException : public xbt_ex {
126 NetworkFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
128 category = network_error;
132 /** Exception raised when something got canceled before completion */
133 class CancelException : public xbt_ex {
136 } // namespace simgrid