Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make capture list explicit.
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_EXCEPTIONS_HPP
7 #define SIMGRID_EXCEPTIONS_HPP
8
9 /** @file exception.hpp SimGrid-specific Exceptions
10  *
11  *  Defines all possible exception that could occur in a SimGrid library.
12  */
13
14 #include <xbt/backtrace.hpp>
15 #include <xbt/ex.h>
16
17 #include <atomic>
18 #include <stdexcept>
19 #include <string>
20
21 namespace simgrid {
22 namespace xbt {
23
24 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
25  *
26  *  Constitute the contextual information of where an exception was thrown
27  *
28  *  These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
29  *  are best created with @ref XBT_THROW_POINT.
30  *
31  *  @ingroup XBT_ex
32  */
33 class ThrowPoint {
34 public:
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)
38       , line_(line)
39       , function_(function)
40       , backtrace_(std::move(bt))
41       , procname_(std::move(actor_name))
42       , pid_(pid)
43   {
44   }
45
46   const char* file_     = nullptr;
47   int line_             = 0;
48   const char* function_ = nullptr;
49   Backtrace backtrace_;
50   std::string procname_ = ""; /**< Name of the process who thrown this */
51   int pid_              = 0;  /**< PID of the process who thrown this */
52 };
53
54 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
55 #define XBT_THROW_POINT                                                                                                \
56   ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid())
57 } // namespace xbt
58
59 /** Ancestor class of all SimGrid exception */
60 class Exception : public std::runtime_error {
61 public:
62   Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
63       : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
64   {
65   }
66
67   /** Return the information about where the exception was thrown */
68   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
69
70   std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
71
72 private:
73   xbt::ThrowPoint throwpoint_;
74 };
75
76 } // namespace simgrid
77
78 /** A legacy exception
79  *
80  *  It is defined by a category and a value within that category (as well as
81  *  an optional error message).
82  *
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.
88  *
89  *  @ingroup XBT_ex_c
90  */
91 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
92 public:
93   /**
94    *
95    * @param throwpoint Throw point (use XBT_THROW_POINT)
96    * @param message    Exception message
97    */
98   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message)
99       : simgrid::Exception(std::move(throwpoint), std::move(message))
100   {
101   }
102
103   xbt_ex(const xbt_ex&) = default;
104
105   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
106
107   /** Category (what went wrong) */
108   xbt_errcat_t category = unknown_error;
109
110   /** Why did it went wrong */
111   int value = 0;
112 };
113
114 namespace simgrid {
115
116 /** Exception raised when a timeout elapsed */
117 class TimeoutError : public xbt_ex {
118 public:
119   TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message)
120       : xbt_ex(std::move(throwpoint), std::move(message))
121   {
122     category = timeout_error;
123   }
124 };
125
126 /** Exception raised when a host fails */
127 class HostFailureException : public xbt_ex {
128 public:
129   HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
130       : xbt_ex(std::move(throwpoint), std::move(message))
131   {
132     category = host_error;
133   }
134 };
135
136 /** Exception raised when a communication fails because of the network or because of the remote host */
137 class NetworkFailureException : public xbt_ex {
138 public:
139   NetworkFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
140       : xbt_ex(std::move(throwpoint), std::move(message))
141   {
142     category = network_error;
143   }
144 };
145
146 /** Exception raised when a storage fails */
147 class StorageFailureException : public xbt_ex {
148 public:
149   StorageFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
150       : xbt_ex(std::move(throwpoint), std::move(message))
151   {
152     category = io_error;
153   }
154 };
155
156 /** Exception raised when something got canceled before completion */
157 class CancelException : public xbt_ex {
158 public:
159   CancelException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
160       : xbt_ex(std::move(throwpoint), std::move(message))
161   {
162     category = cancel_error;
163   }
164 };
165
166 } // namespace simgrid
167
168 #endif