Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename xbt::WithContextException into xbt::ContextedException
[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 <simgrid/exception.hpp>
16 #include <stdexcept>
17 #include <xbt/exception.hpp>
18
19 #include <xbt/ex.h>
20
21 namespace simgrid {
22
23 /** Ancestor class of all SimGrid exception */
24 class exception : public std::runtime_error {
25 public:
26   exception() : std::runtime_error("") {}
27   exception(const char* message) : std::runtime_error(message) {}
28 };
29
30 /** Exception raised when a timeout elapsed */
31 class timeout_error : public simgrid::exception {
32 };
33
34 /** Exception raised when an host fails */
35 class host_failure : public simgrid::exception {
36 };
37
38 /** Exception raised when a communication fails because of the network */
39 class network_failure : public simgrid::exception {
40 };
41
42 /** Exception raised when something got canceled before completion */
43 class canceled_error : public simgrid::exception {
44 };
45
46 } // namespace simgrid
47
48 /** A legacy exception
49  *
50  *  It is defined by a category and a value within that category (as well as
51  *  an optional error message).
52  *
53  *  This used to be a structure for C exceptions but it has been retrofitted
54  *  as a C++ exception and some of its data has been moved in the
55  *  @ref WithContextException base class. We should deprecate it and replace it
56  *  with either C++ different exceptions or `std::system_error` which already
57  *  provides this (category + error code) logic.
58  *
59  *  @ingroup XBT_ex_c
60  */
61 class XBT_PUBLIC xbt_ex : public simgrid::exception, public simgrid::xbt::ContextedException {
62 public:
63   xbt_ex() : simgrid::exception() {}
64
65   /**
66    *
67    * @param throwpoint Throw point (use XBT_THROW_POINT)
68    * @param message    Exception message
69    */
70   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message)
71       : simgrid::exception(message), simgrid::xbt::ContextedException(throwpoint, simgrid::xbt::backtrace())
72   {
73   }
74
75   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
76
77   /** Category (what went wrong) */
78   xbt_errcat_t category = unknown_error;
79
80   /** Why did it went wrong */
81   int value = 0;
82 };
83
84 #endif