Logo AND Algorithmique Numérique Distribuée

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