Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
_SIMIX_cond_wait becomes ConditionVariable::wait
[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__, std::move(simgrid::xbt::Backtrace()), xbt_procname(),       \
57                              xbt_getpid())
58 } // namespace xbt
59
60 /** Ancestor class of all SimGrid exception */
61 class Exception : public std::runtime_error {
62 public:
63   Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
64       : std::runtime_error(std::move(message)), throwpoint_(std::move(throwpoint))
65   {
66   }
67
68   /** Return the information about where the exception was thrown */
69   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
70
71   std::string const resolve_backtrace() const { return throwpoint_.backtrace_.resolve(); }
72
73 private:
74   xbt::ThrowPoint throwpoint_;
75 };
76
77 } // namespace simgrid
78
79 /** A legacy exception
80  *
81  *  It is defined by a category and a value within that category (as well as
82  *  an optional error message).
83  *
84  *  This used to be a structure for C exceptions but it has been retrofitted
85  *  as a C++ exception and some of its data has been moved in the
86  *  @ref WithContextException base class. We should deprecate it and replace it
87  *  with either C++ different exceptions or `std::system_error` which already
88  *  provides this (category + error code) logic.
89  *
90  *  @ingroup XBT_ex_c
91  */
92 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
93 public:
94   /**
95    *
96    * @param throwpoint Throw point (use XBT_THROW_POINT)
97    * @param message    Exception message
98    */
99   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message)
100       : simgrid::Exception(std::move(throwpoint), std::move(message))
101   {
102   }
103
104   xbt_ex(const xbt_ex&) = default;
105
106   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
107
108   /** Category (what went wrong) */
109   xbt_errcat_t category = unknown_error;
110
111   /** Why did it went wrong */
112   int value = 0;
113 };
114
115 namespace simgrid {
116
117 /** Exception raised when a timeout elapsed */
118 class TimeoutError : public xbt_ex {
119 public:
120   TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message)
121       : xbt_ex(std::move(throwpoint), std::move(message))
122   {
123     category = timeout_error;
124   }
125 };
126
127 /** Exception raised when a host fails */
128 class HostFailureException : public xbt_ex {
129 public:
130   HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
131       : xbt_ex(std::move(throwpoint), std::move(message))
132   {
133     category = host_error;
134   }
135 };
136
137 /** Exception raised when a communication fails because of the network */
138 class NetworkFailureException : public xbt_ex {
139 public:
140   NetworkFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
141       : xbt_ex(std::move(throwpoint), std::move(message))
142   {
143     category = network_error;
144   }
145 };
146
147 /** Exception raised when something got canceled before completion */
148 class CancelException : public xbt_ex {
149 public:
150   CancelException(simgrid::xbt::ThrowPoint throwpoint, std::string message)
151       : xbt_ex(std::move(throwpoint), std::move(message))
152   {
153     category = cancel_error;
154   }
155 };
156
157 } // namespace simgrid
158
159 #endif