aGrUM 3.1.1
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// WARNING : Do not include this file directly : instead include
50// <agrum/config.h>
51
52#include <format>
53#include <iomanip>
54#include <iostream>
55#include <sstream>
56#include <string>
57#include <utility>
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#define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG) \
95 class TYPE: public SUPERCLASS { \
96 public: \
97 explicit TYPE(const std::string& aMsg, const std::string& aType = MSG) : \
98 SUPERCLASS(aMsg, aType) {} \
99 TYPE(const TYPE& src) : SUPERCLASS(src) {} \
100 };
101
102#ifdef GUM_FOR_SWIG
103# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
104 { \
105 std::ostringstream error_stream; \
106 error_stream << msg; \
107 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
108 }
109#else // GUM_FOR_SWIG
110# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
111 { \
112 std::ostringstream error_stream; \
113 error_stream << msg; \
114 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
115 }
116#endif // GUM_FOR_SWIG
117
118namespace gum {
122 class Exception: public std::exception {
123 protected:
124 std::string msg_;
125 std::string type_;
126 std::string callstack_;
127 std::string what_;
128
129 public:
130 // ====================================================================
132 // ====================================================================
134 explicit Exception(std::string_view aMsg = "", std::string_view aType = "Generic error");
135
137
138 ~Exception() override = default;
139
141
142 GUM_NODISCARD const char* what() const noexcept override;
143
148 GUM_NODISCARD std::string errorContent() const;
149
154 GUM_NODISCARD std::string errorType() const;
155
160 GUM_NODISCARD std::string errorCallStack() const;
161 };
162
163
169 class FatalError;
170
176 class IteratorError;
177
184
191
197 class NullElement;
198
204 class UndefinedElement;
205
211 class SizeError;
212
213
221
228 class InvalidArgument;
229
235 class IOError;
236
242 class FormatNotFound;
243
250
256 class NotFound;
257
263 class ArgumentError;
264
270 class OutOfBounds;
271
277 class DuplicateElement;
278
284 class DuplicateLabel;
285
287
292 class GraphError;
293
299 class NoNeighbour;
300
306 class NoParent;
307
313 class NoChild;
314
320 class InvalidEdge;
321
327 class InvalidArc;
328
334 class InvalidNode;
335
341 class DefaultInLabel;
342
349
351
356 class CPTError;
357
359
365
372
379
386
393
400
407
414
420
426 class FactoryError;
427
434
440 class TypeError;
441
447 class WrongClassElement;
448
454 class PRMTypeError;
455
461 class LearningError;
462
469
477
483 class DatabaseError;
484
491
498
505
506
513 class SyntaxError;
514
520 class NotImplementedYet;
521
528 class HedgeException;
529
530#ifndef DOXYGEN_SHOULD_SKIP_THIS
531 std::string _createMsg_(std::string_view filename,
532 std::string_view function,
533 int line,
534 std::string_view msg);
535
536 GUM_MAKE_ERROR(FatalError, Exception, "Fatal error")
537
538 GUM_MAKE_ERROR(NotImplementedYet, Exception, "Not implemented yet")
539
540 GUM_MAKE_ERROR(IteratorError, Exception, "Error in iterator")
541
542 GUM_MAKE_ERROR(UndefinedIteratorValue, IteratorError, "Undefined iterator")
543
544 GUM_MAKE_ERROR(UndefinedIteratorKey, IteratorError, "Undefined iterator's key")
545
546 GUM_MAKE_ERROR(NullElement, Exception, "Null element")
547
548 GUM_MAKE_ERROR(UndefinedElement, Exception, "Undefined element")
549
550 GUM_MAKE_ERROR(SizeError, Exception, "incorrect size")
551
552 GUM_MAKE_ERROR(ArgumentError, Exception, "Argument error")
553
554 GUM_MAKE_ERROR(InvalidArgumentsNumber, ArgumentError, "Invalid argument number")
555
556 GUM_MAKE_ERROR(InvalidArgument, ArgumentError, "Invalid argument")
557
558 GUM_MAKE_ERROR(IOError, Exception, "I/O Error")
559
560 GUM_MAKE_ERROR(FormatNotFound, IOError, "Format not found")
561
562 GUM_MAKE_ERROR(OperationNotAllowed, Exception, "Operation not allowed")
563
564 GUM_MAKE_ERROR(NotFound, Exception, "Object not found")
565
566 GUM_MAKE_ERROR(OutOfBounds, ArgumentError, "Out of bound error")
567
568 GUM_MAKE_ERROR(DuplicateElement, ArgumentError, "Duplicate element")
569
570 GUM_MAKE_ERROR(DuplicateLabel, ArgumentError, "Duplicate label")
571
572 GUM_MAKE_ERROR(GraphError, Exception, "Graph error")
573
575
576 GUM_MAKE_ERROR(NoNeighbour, GraphError, "No neighbour found")
577
578 GUM_MAKE_ERROR(NoParent, GraphError, "No parent found")
579
580 GUM_MAKE_ERROR(NoChild, GraphError, "No child found")
581
582 GUM_MAKE_ERROR(InvalidEdge, GraphError, "Invalid edge")
583
584 GUM_MAKE_ERROR(InvalidArc, GraphError, "Invalid arc")
585
586 GUM_MAKE_ERROR(InvalidNode, GraphError, "Invalid node")
587
588 GUM_MAKE_ERROR(DefaultInLabel, GraphError, "Error on label")
589
590 GUM_MAKE_ERROR(InvalidDirectedCycle, GraphError, "Directed cycle detected")
591
592 GUM_MAKE_ERROR(InvalidPartiallyDirectedCycle, GraphError, "Partially directed cycle detected")
593
594 GUM_MAKE_ERROR(CPTError, Exception, "CPT error")
595
596 GUM_MAKE_ERROR(ScheduleMultiDimError, Exception, "ScheduleMultiDim error")
597
600 "The Schedule MultiDim Table is abstract")
601
604 "The Schedule MultiDim Table is unknown")
605
608 "There exists another identical Schedule MultiDim Table")
609
610 GUM_MAKE_ERROR(ScheduleOperationError, Exception, "ScheduleOperator error")
611
614 "The Schedule operation is unknown")
615
618 "The Schedule operation is not available yet")
619
622 "The Schedule operation has not been executed yet")
623
625 Exception,
626 "Several evidence/CPT are incompatible together (proba=0)")
627
628 GUM_MAKE_ERROR(FactoryError, Exception, "Factory error")
629
630 GUM_MAKE_ERROR(FactoryInvalidState, FactoryError, "Invalid state error")
631
633
634 GUM_MAKE_ERROR(WrongClassElement, FactoryError, "Wrong ClassElement")
635
636 GUM_MAKE_ERROR(PRMTypeError, FactoryError, "Wrong subtype or subclass")
637
638 GUM_MAKE_ERROR(LearningError, Exception, "Factory error")
639
640 GUM_MAKE_ERROR(IncompatibleScorePrior, LearningError, "Incompatible (maybe implicit) priors")
641
644 "Possible incompatibility between score and prior")
645
646 GUM_MAKE_ERROR(DatabaseError, LearningError, "Database error")
647
648 GUM_MAKE_ERROR(MissingVariableInDatabase, LearningError, "Missing variable name in database")
649
650 GUM_MAKE_ERROR(MissingValueInDatabase, LearningError, "The database contains some missing values")
651
652 GUM_MAKE_ERROR(UnknownLabelInDatabase, LearningError, "Unknown label found in database")
653
654 class SyntaxError final: public IOError {
655 protected:
656 Size noLine_;
657 Size noCol_;
658 std::string filename_;
659
660 public:
661 SyntaxError(const std::string& aMsg,
662 std::string aFilename,
663 Size nol,
664 Size noc,
665 const std::string& aType = "Syntax Error");
666
667 GUM_NODISCARD Size col() const;
668
669 GUM_NODISCARD Size line() const;
670
671 GUM_NODISCARD std::string filename() const;
672 };
673#endif // DOXYGEN_SHOULD_SKIP_THIS
674} /* namespace gum */
675
676#ifndef DOXYGEN_SHOULD_SKIP_THIS
677# ifndef GUM_NO_INLINE
679# endif // GUM_NO_INLINE
680#endif // DOXYGEN_SHOULD_SKIP_THIS
681
682#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:126
std::string msg_
Definition exceptions.h:124
~Exception() override=default
Exception(const Exception &e)
GUM_NODISCARD std::string errorCallStack() const
Returns the error call stack.
std::string what_
Definition exceptions.h:127
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:125
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:94
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