aGrUM 2.3.2
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-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
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 <iomanip>
53#include <iostream>
54#include <string>
55#include <utility>
56
58
59#define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
60
61#if defined(SWIG) || defined(GUM_FOR_SWIG)
62// msg can be a string or a sequence "x<<y<<z"
63# define GUM_ERROR(type, msg) \
64 { \
65 std::ostringstream err_stream; \
66 err_stream << msg; \
67 throw(type(err_stream.str())); \
68 }
69# define GUM_SHOWERROR(e) \
70 { std::cout << std::endl << (e).what() << std::endl; }
71#else // defined(SWIG) || defined(GUM_FOR_SWIG)
72# define GUM_ERROR(type, msg) \
73 { \
74 std::ostringstream error_stream; \
75 error_stream << msg; \
76 throw(type(gum::_createMsg_(__FILE__, __FUNCTION__, __LINE__, error_stream.str()))); \
77 }
78# ifdef GUM_DEBUG_MODE
79# define GUM_SHOWERROR(e) \
80 { \
81 std::cout << std::endl << __FILE__ << ":" << __LINE__ << " " << (e).what() << std::endl; \
82 std::cout << (e).errorCallStack() << std::endl; \
83 }
84# else // GUM_DEBUG_MODE
85# define GUM_SHOWERROR(e) \
86 { std::cout << std::endl << __FILE__ << ":" << __LINE__ << " " << (e).what() << std::endl; }
87# endif // GUM_DEBUG_MODE
88#endif // defined(SWIG) || defined(GUM_FOR_SWIG)
89
90#define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG) \
91 class TYPE: public SUPERCLASS { \
92 public: \
93 explicit TYPE(const std::string& aMsg, const std::string& aType = MSG) : \
94 SUPERCLASS(aMsg, aType) {}; \
95 TYPE(const TYPE& src) : SUPERCLASS(src) {}; \
96 };
97
98#ifdef GUM_FOR_SWIG
99# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
100 { \
101 std::ostringstream error_stream; \
102 error_stream << msg; \
103 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
104 }
105#else // GUM_FOR_SWIG
106# define GUM_SYNTAX_ERROR(msg, filename, line, column) \
107 { \
108 std::ostringstream error_stream; \
109 error_stream << msg; \
110 throw(gum::SyntaxError(error_stream.str(), filename, line, column)); \
111 }
112#endif // GUM_FOR_SWIG
113
114namespace gum {
118 class Exception: public std::exception {
119 protected:
120 std::string msg_;
121 std::string type_;
122 std::string callstack_;
123 std::string what_;
124
125 public:
126 // ====================================================================
128 // ====================================================================
130 explicit Exception(std::string aMsg = "", std::string aType = "Generic error");
131
133
134 virtual ~Exception() = default;
135
137
138 GUM_NODISCARD const char* what() const noexcept override { return what_.c_str(); };
139
144 GUM_NODISCARD std::string errorContent() const { return msg_; };
145
150 GUM_NODISCARD std::string errorType() const { return type_; };
151
156 GUM_NODISCARD std::string errorCallStack() const { return callstack_; };
157 };
158
159
165 class FatalError;
166
172 class IteratorError;
173
180
187
193 class NullElement;
194
200 class UndefinedElement;
201
207 class SizeError;
208
209
217
224 class InvalidArgument;
225
231 class IOError;
232
238 class FormatNotFound;
239
246
252 class NotFound;
253
259 class ArgumentError;
260
266 class OutOfBounds;
267
273 class DuplicateElement;
274
280 class DuplicateLabel;
281
283
288 class GraphError;
289
295 class NoNeighbour;
296
302 class NoParent;
303
309 class NoChild;
310
316 class InvalidEdge;
317
323 class InvalidArc;
324
330 class InvalidNode;
331
337 class DefaultInLabel;
338
345
347
352 class CPTError;
353
355
361
368
375
382
389
396
403
410
416
422 class FactoryError;
423
430
436 class TypeError;
437
443 class WrongClassElement;
444
450 class PRMTypeError;
451
457 class LearningError;
458
465
473
479 class DatabaseError;
480
487
494
501
502
509 class SyntaxError;
510
516 class NotImplementedYet;
517
518
519#ifndef DOXYGEN_SHOULD_SKIP_THIS
520 std::string _createMsg_(const std::string& filename,
521 const std::string& function,
522 int line,
523 const std::string& msg);
524
525 GUM_MAKE_ERROR(FatalError, Exception, "Fatal error")
526
527 GUM_MAKE_ERROR(NotImplementedYet, Exception, "Not implemented yet")
528
529 GUM_MAKE_ERROR(IteratorError, Exception, "Error in iterator")
530
531 GUM_MAKE_ERROR(UndefinedIteratorValue, IteratorError, "Undefined iterator")
532
533 GUM_MAKE_ERROR(UndefinedIteratorKey, IteratorError, "Undefined iterator's key")
534
535 GUM_MAKE_ERROR(NullElement, Exception, "Null element")
536
537 GUM_MAKE_ERROR(UndefinedElement, Exception, "Undefined element")
538
539 GUM_MAKE_ERROR(SizeError, Exception, "incorrect size")
540
541 GUM_MAKE_ERROR(ArgumentError, Exception, "Argument error")
542
543 GUM_MAKE_ERROR(InvalidArgumentsNumber, ArgumentError, "Invalid argument number")
544
545 GUM_MAKE_ERROR(InvalidArgument, ArgumentError, "Invalid argument")
546
547 GUM_MAKE_ERROR(IOError, Exception, "I/O Error")
548
549 GUM_MAKE_ERROR(FormatNotFound, IOError, "Format not found")
550
551 GUM_MAKE_ERROR(OperationNotAllowed, Exception, "Operation not allowed")
552
553 GUM_MAKE_ERROR(NotFound, Exception, "Object not found")
554
555 GUM_MAKE_ERROR(OutOfBounds, ArgumentError, "Out of bound error")
556
557 GUM_MAKE_ERROR(DuplicateElement, ArgumentError, "Duplicate element")
558
559 GUM_MAKE_ERROR(DuplicateLabel, ArgumentError, "Duplicate label")
560
561 GUM_MAKE_ERROR(GraphError, Exception, "Graph error")
562
563 GUM_MAKE_ERROR(NoNeighbour, GraphError, "No neighbour found")
564
565 GUM_MAKE_ERROR(NoParent, GraphError, "No parent found")
566
567 GUM_MAKE_ERROR(NoChild, GraphError, "No child found")
568
569 GUM_MAKE_ERROR(InvalidEdge, GraphError, "Invalid edge")
570
571 GUM_MAKE_ERROR(InvalidArc, GraphError, "Invalid arc")
572
573 GUM_MAKE_ERROR(InvalidNode, GraphError, "Invalid node")
574
575 GUM_MAKE_ERROR(DefaultInLabel, GraphError, "Error on label")
576
577 GUM_MAKE_ERROR(InvalidDirectedCycle, GraphError, "Directed cycle detected")
578
579 GUM_MAKE_ERROR(InvalidPartiallyDirectedCycle, GraphError, "Partially directed cycle detected")
580
581 GUM_MAKE_ERROR(CPTError, Exception, "CPT error")
582
583 GUM_MAKE_ERROR(ScheduleMultiDimError, Exception, "ScheduleMultiDim error")
584
587 "The Schedule MultiDim Table is abstract")
588
591 "The Schedule MultiDim Table is unknown")
592
595 "There exists another identical Schedule MultiDim Table")
596
597 GUM_MAKE_ERROR(ScheduleOperationError, Exception, "ScheduleOperator error")
598
601 "The Schedule operation is unknown")
602
605 "The Schedule operation is not available yet")
606
609 "The Schedule operation has not been executed yet")
610
612 Exception,
613 "Several evidence/CPT are incompatible together (proba=0)")
614
615 GUM_MAKE_ERROR(FactoryError, Exception, "Factory error")
616
617 GUM_MAKE_ERROR(FactoryInvalidState, FactoryError, "Invalid state error")
618
620
621 GUM_MAKE_ERROR(WrongClassElement, FactoryError, "Wrong ClassElement")
622
623 GUM_MAKE_ERROR(PRMTypeError, FactoryError, "Wrong subtype or subclass")
624
625 GUM_MAKE_ERROR(LearningError, Exception, "Factory error")
626
627 GUM_MAKE_ERROR(IncompatibleScorePrior, LearningError, "Incompatible (maybe implicit) priors")
628
631 "Possible incompatibility between score and prior")
632
633 GUM_MAKE_ERROR(DatabaseError, LearningError, "Database error")
634
635 GUM_MAKE_ERROR(MissingVariableInDatabase, LearningError, "Missing variable name in database")
636
637 GUM_MAKE_ERROR(MissingValueInDatabase, LearningError, "The database contains some missing values")
638
639 GUM_MAKE_ERROR(UnknownLabelInDatabase, LearningError, "Unknown label found in database")
640
641 class SyntaxError final: public IOError {
642 protected:
643 Size noLine_;
644 Size noCol_;
645 std::string filename_;
646
647 public:
648 SyntaxError(const std::string& aMsg,
649 std::string aFilename,
650 Size nol,
651 Size noc,
652 const std::string& aType = "Syntax Error") :
653 IOError(aMsg, aType), noLine_(nol), noCol_(noc), filename_(std::move(aFilename)) {
654# ifdef GUM_FOR_SWIG
655 what_ = "[pyAgrum] " + msg_;
656# else // GUM_FOR_SWIG
657 std::ostringstream error_stream;
658 error_stream << type_ << ":" << std::endl;
659 error_stream << filename() << ":" << line() << "," << col() << " : " << msg_;
660 what_ = error_stream.str();
661# endif // GUM_FOR_SWIG
662 }
663
664 GUM_NODISCARD Size col() const { return noCol_; };
665
666 GUM_NODISCARD Size line() const { return noLine_; };
667
668 GUM_NODISCARD std::string filename() const { return filename_; };
669 };
670#endif // DOXYGEN_SHOULD_SKIP_THIS
671} /* namespace gum */
672
673#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.
virtual ~Exception()=default
GUM_NODISCARD const char * what() const noexcept override
Definition exceptions.h:138
GUM_NODISCARD std::string errorType() const
Returns the error type.
Definition exceptions.h:150
std::string callstack_
Definition exceptions.h:122
GUM_NODISCARD std::string errorCallStack() const
Returns the error call stack.
Definition exceptions.h:156
Exception(std::string aMsg="", std::string aType="Generic error")
std::string msg_
Definition exceptions.h:120
GUM_NODISCARD std::string errorContent() const
Returns the message content.
Definition exceptions.h:144
Exception(const Exception &e)
std::string what_
Definition exceptions.h:123
std::string type_
Definition exceptions.h:121
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 : 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:90
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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