Logo AND Algorithmique Numérique Distribuée

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