aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
exceptions.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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
47#ifndef GUM_EXCEPTIONS_H
48#define GUM_EXCEPTIONS_H
49
50#include <format>
51#include <iomanip>
52#include <iostream>
53#include <sstream>
54#include <string>
55#include <utility>
56
57#include <agrum/config.h>
58
60
61#include <string_view>
62
63#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
64
65#if defined(SWIG) || defined(GUM_FOR_SWIG)
66// msg can be a string or a sequence "x<<y<<z"
67# define GUM_ERROR(type, msg) \
68 { \
69 std::ostringstream err_stream; \
70 err_stream << msg; \
71 throw(type(err_stream.str())); \
72 }
73# define GUM_SHOWERROR(e) \
74 { std::cout << std::endl << (e).what() << std::endl; }
75#else // defined(SWIG) || defined(GUM_FOR_SWIG)
76# define GUM_ERROR(type, msg) \
77 { \
78 std::ostringstream error_stream; \
79 error_stream << msg; \
80 throw(type(gum::_createMsg_(__FILE__, __FUNCTION__, __LINE__, error_stream.str()))); \
81 }
82# ifdef GUM_DEBUG_MODE
83# define GUM_SHOWERROR(e) \
84 { \
85 std::cout << std::endl << __FILE__ << ":" << __LINE__ << " " << (e).what() << std::endl; \
86 std::cout << (e).errorCallStack() << std::endl; \
87 }
88# else // GUM_DEBUG_MODE
89# define GUM_SHOWERROR(e) \
90 { std::cout << std::endl << __FILE__ << ":" << __LINE__ << " " << (e).what() << std::endl; }
91# endif // GUM_DEBUG_MODE
92#endif // defined(SWIG) || defined(GUM_FOR_SWIG)
93
94#ifdef GUM_FOR_SWIG
95# define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG) \
96 class GUM_SHARED_PUBLIC TYPE: public SUPERCLASS { \
97 public: \
98 explicit TYPE(const std::string& aMsg, const std::string& aType = MSG) : \
99 SUPERCLASS(aMsg, aType) {} \
100 TYPE(const TYPE& src) : SUPERCLASS(src) {} \
101 const char* pythonClassName_() const noexcept override { return #TYPE; } \
102 };
103#else // GUM_FOR_SWIG
104# define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG) \
105 class GUM_SHARED_PUBLIC TYPE: public SUPERCLASS { \
106 public: \
107 explicit TYPE(const std::string& aMsg, const std::string& aType = MSG) : \
108 SUPERCLASS(aMsg, aType) {} \
109 TYPE(const TYPE& src) : SUPERCLASS(src) {} \
110 };
111#endif // GUM_FOR_SWIG
112
113#ifdef GUM_FOR_SWIG
114# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
115 { \
116 std::ostringstream error_stream; \
117 error_stream << msg; \
118 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
119 }
120#else // GUM_FOR_SWIG
121# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
122 { \
123 std::ostringstream error_stream; \
124 error_stream << msg; \
125 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
126 }
127#endif // GUM_FOR_SWIG
128
129namespace gum {
133 class GUM_SHARED_PUBLIC Exception: public std::exception {
134 protected:
135 std::string msg_;
136 std::string type_;
137 std::string callstack_;
138 std::string what_;
139
140 public:
141 // ====================================================================
143 // ====================================================================
145 explicit Exception(std::string_view aMsg = "", std::string_view aType = "Generic error");
146
148
149 ~Exception() override = default;
150
152
153 GUM_NODISCARD const char* what() const noexcept override;
154
159 GUM_NODISCARD std::string errorContent() const;
160
165 GUM_NODISCARD std::string errorType() const;
166
171 GUM_NODISCARD std::string errorCallStack() const;
172
173#ifdef GUM_FOR_SWIG
183 GUM_NODISCARD virtual const char* pythonClassName_() const noexcept;
184#endif // GUM_FOR_SWIG
185 };
186
187
193 class FatalError;
194
200 class IteratorError;
201
208
215
221 class NullElement;
222
228 class UndefinedElement;
229
235 class SizeError;
236
237
245
252 class InvalidArgument;
253
259 class IOError;
260
266 class FormatNotFound;
267
274
280 class NotFound;
281
287 class ArgumentError;
288
294 class OutOfBounds;
295
301 class DuplicateElement;
302
308 class DuplicateLabel;
309
311
316 class GraphError;
317
323 class NoNeighbour;
324
330 class NoParent;
331
337 class NoChild;
338
344 class InvalidEdge;
345
351 class InvalidArc;
352
358 class InvalidNode;
359
365 class DefaultInLabel;
366
373
375
380 class CPTError;
381
383
389
396
403
410
417
424
431
438
444
450 class FactoryError;
451
458
464 class TypeError;
465
471 class WrongClassElement;
472
478 class PRMTypeError;
479
485 class LearningError;
486
493
501
507 class DatabaseError;
508
515
522
529
530
537 class SyntaxError;
538
544 class NotImplementedYet;
545
552 class HedgeException;
553
554#ifndef DOXYGEN_SHOULD_SKIP_THIS
555 GUM_SHARED_PUBLIC std::string _createMsg_(std::string_view filename,
556 std::string_view function,
557 int line,
558 std::string_view msg);
559
560 GUM_MAKE_ERROR(FatalError, Exception, "Fatal error")
561
562 GUM_MAKE_ERROR(NotImplementedYet, Exception, "Not implemented yet")
563
564 GUM_MAKE_ERROR(IteratorError, Exception, "Error in iterator")
565
566 GUM_MAKE_ERROR(UndefinedIteratorValue, IteratorError, "Undefined iterator")
567
568 GUM_MAKE_ERROR(UndefinedIteratorKey, IteratorError, "Undefined iterator's key")
569
570 GUM_MAKE_ERROR(NullElement, Exception, "Null element")
571
572 GUM_MAKE_ERROR(UndefinedElement, Exception, "Undefined element")
573
574 GUM_MAKE_ERROR(SizeError, Exception, "incorrect size")
575
576 GUM_MAKE_ERROR(ArgumentError, Exception, "Argument error")
577
578 GUM_MAKE_ERROR(InvalidArgumentsNumber, ArgumentError, "Invalid argument number")
579
580 GUM_MAKE_ERROR(InvalidArgument, ArgumentError, "Invalid argument")
581
582 GUM_MAKE_ERROR(IOError, Exception, "I/O Error")
583
584 GUM_MAKE_ERROR(FormatNotFound, IOError, "Format not found")
585
586 GUM_MAKE_ERROR(OperationNotAllowed, Exception, "Operation not allowed")
587
588 GUM_MAKE_ERROR(NotFound, Exception, "Object not found")
589
590 GUM_MAKE_ERROR(OutOfBounds, ArgumentError, "Out of bound error")
591
592 GUM_MAKE_ERROR(DuplicateElement, ArgumentError, "Duplicate element")
593
594 GUM_MAKE_ERROR(DuplicateLabel, ArgumentError, "Duplicate label")
595
596 GUM_MAKE_ERROR(GraphError, Exception, "Graph error")
597
599
600 GUM_MAKE_ERROR(NoNeighbour, GraphError, "No neighbour found")
601
602 GUM_MAKE_ERROR(NoParent, GraphError, "No parent found")
603
604 GUM_MAKE_ERROR(NoChild, GraphError, "No child found")
605
606 GUM_MAKE_ERROR(InvalidEdge, GraphError, "Invalid edge")
607
608 GUM_MAKE_ERROR(InvalidArc, GraphError, "Invalid arc")
609
610 GUM_MAKE_ERROR(InvalidNode, GraphError, "Invalid node")
611
612 GUM_MAKE_ERROR(DefaultInLabel, GraphError, "Error on label")
613
614 GUM_MAKE_ERROR(InvalidDirectedCycle, GraphError, "Directed cycle detected")
615
616 GUM_MAKE_ERROR(InvalidPartiallyDirectedCycle, GraphError, "Partially directed cycle detected")
617
618 GUM_MAKE_ERROR(CPTError, Exception, "CPT error")
619
620 GUM_MAKE_ERROR(ScheduleMultiDimError, Exception, "ScheduleMultiDim error")
621
624 "The Schedule MultiDim Table is abstract")
625
628 "The Schedule MultiDim Table is unknown")
629
632 "There exists another identical Schedule MultiDim Table")
633
634 GUM_MAKE_ERROR(ScheduleOperationError, Exception, "ScheduleOperator error")
635
638 "The Schedule operation is unknown")
639
642 "The Schedule operation is not available yet")
643
646 "The Schedule operation has not been executed yet")
647
649 Exception,
650 "Several evidence/CPT are incompatible together (proba=0)")
651
652 GUM_MAKE_ERROR(FactoryError, Exception, "Factory error")
653
654 GUM_MAKE_ERROR(FactoryInvalidState, FactoryError, "Invalid state error")
655
657
658 GUM_MAKE_ERROR(WrongClassElement, FactoryError, "Wrong ClassElement")
659
660 GUM_MAKE_ERROR(PRMTypeError, FactoryError, "Wrong subtype or subclass")
661
662 GUM_MAKE_ERROR(LearningError, Exception, "Factory error")
663
664 GUM_MAKE_ERROR(IncompatibleScorePrior, LearningError, "Incompatible (maybe implicit) priors")
665
668 "Possible incompatibility between score and prior")
669
670 GUM_MAKE_ERROR(DatabaseError, LearningError, "Database error")
671
672 GUM_MAKE_ERROR(MissingVariableInDatabase, LearningError, "Missing variable name in database")
673
674 GUM_MAKE_ERROR(MissingValueInDatabase, LearningError, "The database contains some missing values")
675
676 GUM_MAKE_ERROR(UnknownLabelInDatabase, LearningError, "Unknown label found in database")
677
678 class GUM_SHARED_PUBLIC SyntaxError final: public IOError {
679 protected:
680 Size noLine_;
681 Size noCol_;
682 std::string filename_;
683
684 public:
685 SyntaxError(const std::string& aMsg,
686 std::string aFilename,
687 Size nol,
688 Size noc,
689 const std::string& aType = "Syntax Error");
690
691 GUM_NODISCARD Size col() const;
692
693 GUM_NODISCARD Size line() const;
694
695 GUM_NODISCARD std::string filename() const;
696 };
697#endif // DOXYGEN_SHOULD_SKIP_THIS
698} /* namespace gum */
699
700#ifndef DOXYGEN_SHOULD_SKIP_THIS
701# ifndef GUM_NO_INLINE
703# endif // GUM_NO_INLINE
704#endif // DOXYGEN_SHOULD_SKIP_THIS
705
706#endif /* GUM_EXCEPTIONS_H */
Exception : The Schedule MultiDim Table is abstract.
Exception base for argument error.
Exception base for CPT error.
Error: An unknown error occurred while accessing a database.
Exception : default in label.
Exception : a similar element already exists.
Exception : a similar label already exists.
Exception : There exists another identical Schedule MultiDim Table.
Exception(std::string_view aMsg="", std::string_view aType="Generic error")
std::string callstack_
Definition exceptions.h:137
std::string msg_
Definition exceptions.h:135
~Exception() override=default
Exception(const Exception &e)
GUM_NODISCARD std::string errorCallStack() const
Returns the error call stack.
std::string what_
Definition exceptions.h:138
GUM_NODISCARD std::string errorContent() const
Returns the message content.
GUM_NODISCARD const char * what() const noexcept override
GUM_NODISCARD std::string errorType() const
Returns the error type.
std::string type_
Definition exceptions.h:136
Exception base for factory error.
Exception : invalid state error.
Exception : fatal (unknown ?) error.
Exception : a I/O format was not found.
Exception base for graph error.
Exception : "hedge" (witness of non-identifiability) is detected in do-calculus / ID computations.
Exception : input/output problem.
Exception : several evidence are incompatible together (proba=0).
Error: The score already contains a different 'implicit' prior.
Exception : there is something wrong with an arc.
Exception: at least one argument passed to a function is not what was expected.
Exception: the number of arguments passed to a function is not what was expected.
Exception : existence of a directed cycle in a graph.
Exception : there is something wrong with an edge.
Exception : node does not exist.
Exceptions for learning.
Error: The database contains some missing values.
Error: A name of variable is not found in the database.
Exception : no child for a given node was found.
Exception : no neighbour to a given node was found.
Exception : no parent for a given node was found.
Exception : the element we looked for cannot be found.
Exception : there is something wrong with an implementation.
Exception : a pointer or a reference on a nullptr (0) object.
Exception : operation not allowed.
Exception : out of bound.
Exception : wrong subtype or subclass.
Error: Due to its weight, the prior is currently compatible with the score but if you change the weig...
Exception base for ScheduleMultiDim errors.
Exception base for ScheduleOperator errors.
Exception : problem with size.
Special exception for syntax errors in files.
Exception : wrong type for this operation.
Exception : The Schedule Operation is not available yet.
Exception : a looked-for element could not be found.
Exception : iterator does not point to any valid key.
Exception : generic error on iterator.
Exception : The Schedule Operation has not been executed yet.
Error: An unknown label is found in the database.
Exception : The Schedule MultiDim Table is unknown.
Exception : The Schedule Operation is unknown.
Exception: wrong PRMClassElement for this operation.
#define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG)
Definition exceptions.h:104
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
Provides basic types used in aGrUM.
#define GUM_NODISCARD
Definition types.h:61