aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
exceptions.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
42#include <cstdio>
43#include <iostream>
44#include <sstream>
45#include <utility>
46
47#include <agrum/agrum.h>
48
50#ifdef GUM_DEBUG_MODE
51# ifdef HAVE_EXECINFO_H
52# include <execinfo.h>
53# endif // HAVE_EXECINFO_H
54#endif // GUM_DEBUG_MODE
55
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60 std::string _createMsg_(const std::string& filename,
61 const std::string& function,
62 const int line,
63 const std::string& msg) {
64 std::stringstream stream;
65# ifdef GUM_FOR_SWIG
66 stream << std::endl << msg << std::endl;
67# else // GUM_FOR_SWIG
68 stream << std::endl
69 << filename << ":" << line << " in " << function << "()" << std::endl
70 << "--------------" << std::endl
71 << "! " << msg << std::endl
72 << "--------------" << std::endl;
73# endif // GUM_FOR_SWIG
74 return stream.str();
75 }
76
78 std::exception(e), msg_(e.msg_), type_(e.type_), what_(e.what_) {}
79
80 Exception::Exception(std::string aMsg, std::string aType) :
81 std::exception(), msg_(std::move(aMsg)), type_(std::move(aType)) {
82# ifdef GUM_FOR_SWIG
83 what_ = "[pyAgrum] " + type_ + ": " + msg_;
84# else // GUM_FOR_SWIG
85 what_ = type_ + ": " + msg_;
86# endif // GUM_FOR_SWIG
87
88# ifdef GUM_DEBUG_MODE
89# ifdef HAVE_EXECINFO_H
90# define callStackDepth 20
91 void* array[callStackDepth];
92 size_t size;
93 char** strings;
94 size = backtrace(array, callStackDepth);
95 strings = backtrace_symbols(array, size);
96
97 std::stringstream stream;
98
99 for (size_t i = 1; i < size; ++i) {
100 stream << i << " :" << strings[i] << std::endl;
101 }
102
103 free(strings);
104 callstack_ = stream.str();
105# else // HAVE_EXECINFO_H
106 callstack_ = "Callstack only in linux debug mode when execinfo.h available";
107# endif // HAVE_EXECINFO_H
108# else // GUM_DEBUG_MODE
109 callstack_ = "Callstack only in linux debug mod ewhen execinfo.h available";
110# endif // GUM_DEBUG_MODE
111 }
112
113} /* namespace gum */
114
115#endif // DOXYGEN_SHOULD_SKIP_THIS
Base class for all aGrUM's exceptions.
Definition exceptions.h:118
Exception(std::string aMsg="", std::string aType="Generic error")
aGrUM's exceptions
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.