Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement THROW_IMPOSSIBLE and THROW_UNIMPLEMENTED with std::runtime_error directly
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018. 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 <stdexcept>
18 #include <string>
19
20 namespace simgrid {
21 namespace xbt {
22
23 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
24  *
25  *  Constitute the contextual information of where an exception was thrown
26  *
27  *  These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
28  *  are best created with @ref XBT_THROW_POINT.
29  *
30  *  @ingroup XBT_ex
31  */
32 class ThrowPoint {
33 public:
34   ThrowPoint() = default;
35   explicit ThrowPoint(const char* file, int line, const char* function, Backtrace bt, std::string actor_name, int pid)
36       : file_(file), line_(line), function_(function), backtrace_(bt), procname_(actor_name), pid_(pid)
37   {
38   }
39
40   const char* file_     = nullptr;
41   int line_             = 0;
42   const char* function_ = nullptr;
43   Backtrace backtrace_;
44   std::string procname_ = ""; /**< Name of the process who thrown this */
45   int pid_              = 0;  /**< PID of the process who thrown this */
46 };
47
48 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
49 #define XBT_THROW_POINT                                                                                                \
50   ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::backtrace(), xbt_procname(), xbt_getpid())
51 } // namespace xbt
52
53 /** Ancestor class of all SimGrid exception */
54 class Exception : public std::runtime_error {
55 public:
56   Exception(simgrid::xbt::ThrowPoint throwpoint, std::string message)
57       : std::runtime_error(message), throwpoint_(throwpoint)
58   {
59   }
60
61   /** Return the information about where the exception was thrown */
62   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
63
64 private:
65   xbt::ThrowPoint throwpoint_;
66 };
67
68 } // namespace simgrid
69
70 /** A legacy exception
71  *
72  *  It is defined by a category and a value within that category (as well as
73  *  an optional error message).
74  *
75  *  This used to be a structure for C exceptions but it has been retrofitted
76  *  as a C++ exception and some of its data has been moved in the
77  *  @ref WithContextException base class. We should deprecate it and replace it
78  *  with either C++ different exceptions or `std::system_error` which already
79  *  provides this (category + error code) logic.
80  *
81  *  @ingroup XBT_ex_c
82  */
83 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
84 public:
85   /**
86    *
87    * @param throwpoint Throw point (use XBT_THROW_POINT)
88    * @param message    Exception message
89    */
90   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, std::string message) : simgrid::Exception(throwpoint, message) {}
91
92   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
93
94   /** Category (what went wrong) */
95   xbt_errcat_t category = unknown_error;
96
97   /** Why did it went wrong */
98   int value = 0;
99 };
100
101 namespace simgrid {
102
103 /** Exception raised when a timeout elapsed */
104 class TimeoutError : public xbt_ex {
105 public:
106   TimeoutError(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
107   {
108     category = timeout_error;
109   }
110 };
111
112 /** Exception raised when an host fails */
113 class HostFailureException : public xbt_ex {
114 public:
115   HostFailureException(simgrid::xbt::ThrowPoint throwpoint, std::string message) : xbt_ex(throwpoint, message)
116   {
117     category = host_error;
118   }
119 };
120
121 /** Exception raised when a communication fails because of the network */
122 class NetworkFailureException : public xbt_ex {
123 };
124
125 /** Exception raised when something got canceled before completion */
126 class CancelException : public xbt_ex {
127 };
128
129 } // namespace simgrid
130
131 #endif