aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
exceptions.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief aGrUM's exceptions
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_EXCEPTIONS_H
28 #define GUM_EXCEPTIONS_H
29 // WARNING : Do not include this file directly : instead include
30 // <agrum/config.h>
31 
32 #include <iomanip>
33 #include <iostream>
34 #include <string>
35 
36 #include <agrum/tools/core/types.h>
37 
38 #define GUM_ERROR_IN_EXPR(type, msg) throw(type(msg))
39 
40 #ifdef GUM_FOR_SWIG
41 # define GUM_ERROR(type, msg)
42  {
43  std::ostringstream error_stream;
44  error_stream << msg;
45  throw(type(error_stream.str()));
46  }
47 # define GUM_SHOWERROR(e)
48  { std::cout << std::endl << (e).errorType() << " : " << (e).errorContent() << std::endl; }
49 #else
50 # ifndef GUM_DEBUG_MODE
51 # define GUM_ERROR(type, msg)
52  {
53  std::ostringstream error_stream;
54  error_stream << __FILE__ << ":" << __LINE__ << ": " << msg;
55  throw(type(error_stream.str()));
56  }
57 # define GUM_SHOWERROR(e)
58  {
59  std::cout << std::endl
60  << __FILE__ << ":" << __LINE__ << " " << (e).errorType() << " from "
61  << std::endl
62  << (e).errorContent() << std::endl;
63  }
64 # else // GUM_FOR_SWIG
65 # define GUM_ERROR(type, msg)
66  {
67  std::ostringstream error_stream;
68  error_stream << msg;
69  throw(type(gum::_createMsg_(__FILE__, __FUNCTION__, __LINE__, error_stream.str())));
70  }
71 # define GUM_SHOWERROR(e)
72  {
73  std::cout << std::endl
74  << __FILE__ << ":" << __LINE__ << " " << (e).errorType() << " from "
75  << std::endl
76  << (e).errorContent() << std::endl;
77  std::cout << (e).errorCallStack() << std::endl;
78  }
79 # endif // GUM_DEBUG_MODE
80 #endif // GUM_FOR_SWIG
81 
82 #define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG)
83  class TYPE: public SUPERCLASS {
84  public:
85  explicit TYPE(const std::string& aMsg, const std::string& aType = MSG) :
86  SUPERCLASS(aMsg, aType){};
87  TYPE(const TYPE& src) : SUPERCLASS(src){};
88  };
89 
90 #define GUM_SYNTAX_ERROR(msg, line, column)
91  {
92  std::ostringstream error_stream;
93  error_stream << msg;
94  throw(gum::SyntaxError(error_stream.str(), line, column));
95  }
96 
97 namespace gum {
98 
99  /**
100  * @brief Base class for all aGrUM's exceptions.
101  */
102  class Exception {
103  protected:
104  std::string msg_;
105  std::string type_;
106  std::string callstack_;
107 
108  public:
109  // ====================================================================
110  /// @name Class constructors & destructors
111  // ====================================================================
112  /// @{
113  explicit Exception(std::string aMsg = "", std::string aType = "Generic error");
114 
115  Exception(const Exception& e);
116 
117  ~Exception() = default;
118 
119 /// @}
120 #ifdef GUM_FOR_SWIG
121  std::string what() const { return "[pyAgrum] " + type_ + ": " + msg_; }
122 #else // GUM_FOR_SWIG
123  std::string what() const { return type_ + " : " + msg_; }
124 #endif // GUM_FOR_SWIG
125 
126  /**
127  * @brief Returns the message content.
128  * @return Returns the message content.
129  */
130  std::string errorContent() const { return msg_; }
131 
132  /**
133  * @brief Returns the error type.
134  * @return Returns the error type.
135  */
136  std::string errorType() const { return type_; }
137 
138  /**
139  * @brief Returns the error call stack.
140  * @return Returns the error call stack.
141  */
142  std::string errorCallStack() const { return callstack_; }
143  };
144 
145  /**
146  * @class gum::IdError agrum/tools/core/exceptions.h
147  * @extends gum::Exception
148  * Exception : there is a problem with an ID
149  */
150  class IdError;
151 
152  /**
153  * @class gum::FatalError agrum/tools/core/exceptions.h
154  * @extends gum::Exception
155  * Exception : fatal (unknown ?) error
156  */
157  class FatalError;
158 
159  /**
160  * @class gum::UndefinedIteratorValue agrum/tools/core/exceptions.h
161  * @extends gum::Exception
162  * Exception : iterator does not point to any valid value
163  */
164  class UndefinedIteratorValue;
165 
166  /**
167  * @class gum::UndefinedIteratorKey agrum/tools/core/exceptions.h
168  * @extends gum::Exception
169  * Exception : iterator does not point to any valid key
170  */
171  class UndefinedIteratorKey;
172 
173  /**
174  * @class gum::NullElement agrum/tools/core/exceptions.h
175  * @extends gum::Exception
176  * Exception : a pointer or a reference on a nullptr (0) object
177  */
178  class NullElement;
179 
180  /**
181  * @class gum::UndefinedElement agrum/tools/core/exceptions.h
182  * @extends gum::Exception
183  * Exception : a looked-for element could not be found
184  */
185  class UndefinedElement;
186 
187  /**
188  * @class gum::SizeError agrum/tools/core/exceptions.h
189  * @extends gum::Exception
190  * Exception : problem with size
191  */
192  class SizeError;
193 
194  /**
195  * @class gum::EmptySet agrum/tools/core/exceptions.h
196  * @extends gum::Exception
197  * Exception: an empty set is found, where it should not be
198  */
199  class EmptySet;
200 
201  /**
202  * @class gum::InvalidArgumentsNumber agrum/tools/core/exceptions.h
203  * @extends gum::Exception
204  * Exception: the number of arguments passed to a function is not what was
205  * expected
206  */
207  class InvalidArgumentsNumber;
208 
209  /**
210  * @class gum::InvalidArgument agrum/tools/core/exceptions.h
211  * @extends gum::Exception
212  * Exception: at least one argument passed to a function is not what was
213  * expected
214  */
215  class InvalidArgument;
216 
217  /**
218  * @class gum::IOError agrum/tools/core/exceptions.h
219  * @extends gum::Exception
220  * Exception : input/output problem
221  */
222  class IOError;
223 
224  /**
225  * @class gum::FormatNotFound agrum/tools/core/exceptions.h
226  * @extends gum::Exception
227  * Exception : a I/O format was not found
228  */
229  class FormatNotFound;
230 
231  /**
232  * @class gum::OperationNotAllowed agrum/tools/core/exceptions.h
233  * @extends gum::Exception
234  * Exception : operation not allowed
235  */
236  class OperationNotAllowed;
237 
238  /**
239  * @class gum::NotFound agrum/tools/core/exceptions.h
240  * @extends gum::Exception
241  * Exception : the element we looked for cannot be found
242  */
243  class NotFound;
244 
245  /**
246  * @class gum::ReferenceError agrum/tools/core/exceptions.h
247  * @extends gum::Exception
248  * Exception base for reference error
249  */
250  class ReferenceError;
251 
252  /**
253  * @class gum::OutOfBounds agrum/tools/core/exceptions.h
254  * @extends gum::ReferenceError
255  * Exception : out of bound
256  */
257  class OutOfBounds;
258 
259  /**
260  * @class gum::OutOfLowerBound agrum/tools/core/exceptions.h
261  * @extends OutOfBounds
262  * Exception : out of lower bound
263  */
264  class OutOfLowerBound;
265 
266  /**
267  * @class gum::OutOfUpperBound agrum/tools/core/exceptions.h
268  * @extends OutOfBounds
269  * Exception : out of upper bound
270  */
271  class OutOfUpperBound;
272 
273  /**
274  * @class gum::DuplicateElement agrum/tools/core/exceptions.h
275  * @extends gum::ReferenceError
276  * Exception : a similar element already exists
277  */
278  class DuplicateElement;
279 
280  /**
281  * @class gum::DuplicateLabel agrum/tools/core/exceptions.h
282  * @extends gum::ReferenceError
283  * Exception : a similar label already exists
284  */
285  class DuplicateLabel;
286 
287  ///////////////////////////////////
288  /**
289  * @class gum::GraphError agrum/tools/core/exceptions.h
290  * @extends gum::Exception
291  * Exception base for graph error
292  */
293  class GraphError;
294 
295  /**
296  * @class gum::NoNeighbour agrum/tools/core/exceptions.h
297  * @extends gum::GraphError
298  * Exception : no neighbour to a given node was found
299  */
300  class NoNeighbour;
301 
302  /**
303  * @class gum::NoParent agrum/tools/core/exceptions.h
304  * @extends gum::GraphError
305  * Exception : no parent for a given node was found
306  */
307  class NoParent;
308 
309  /**
310  * @class gum::NoChild agrum/tools/core/exceptions.h
311  * @extends gum::GraphError
312  * Exception : no child for a given node was found
313  */
314  class NoChild;
315 
316  /**
317  * @class gum::InvalidEdge agrum/tools/core/exceptions.h
318  * @extends gum::GraphError
319  * Exception : there is something wrong with an edge
320  */
321  class InvalidEdge;
322 
323  /**
324  * @class gum::InvalidArc agrum/tools/core/exceptions.h
325  * @extends gum::GraphError
326  * Exception : there is something wrong with an arc
327  */
328  class InvalidArc;
329 
330  /**
331  * @class gum::InvalidNode agrum/tools/core/exceptions.h
332  * @extends gum::GraphError
333  * Exception : node does not exist
334  */
335  class InvalidNode;
336 
337  /**
338  * @class gum::EmptyBSTree agrum/tools/core/exceptions.h
339  * @extends gum::GraphError
340  * Exception : the binary search tree is empty
341  */
342  class EmptyBSTree;
343 
344  /**
345  * @class gum::DefaultInLabel agrum/tools/core/exceptions.h
346  * @extends gum::GraphError
347  * Exception : default in label
348  */
349  class DefaultInLabel;
350 
351  /**
352  * @class gum::InvalidDirectedCycle agrum/tools/core/exceptions.h
353  * @extends gum::GraphError
354  * Exception : existence of a directed cycle in a graph
355  */
356  class InvalidDirectedCycle;
357 
358  ///////////////////////////////////
359  /**
360  * @class gum::CPTError agrum/tools/core/exceptions.h
361  * @extends gum::Exception
362  * Exception base for CPT error
363  */
364  class CPTError;
365 
366  /**
367  * @class gum::CPTNoSumTo1 agrum/tools/core/exceptions.h
368  * @extends gum::CPTError
369  * Exception : the CPT does not sum to 1
370  */
371  class CPTNoSumTo1;
372 
373  /**
374  * @class gum::IncompatibleEvidence agrum/tools/core/exceptions.h
375  * Exception : several evidence are incompatible together (proba=0)
376  */
377  class IncompatibleEvidence;
378 
379  /**
380  * @class gum::FactoryError agrum/tools/core/exceptions.h
381  * @extends gum::Exception
382  * Exception base for factory error
383  */
384  class FactoryError;
385 
386  /**
387  * @class gum::FactoryInvalidState agrum/tools/core/exceptions.h
388  * @extends gum::FactoryError
389  * Exception : invalid state error
390  */
391  class FactoryInvalidState;
392 
393  /**
394  * @class gum::TypeError agrum/tools/core/exceptions.h
395  * @extends gum::FactoryError
396  * Exception : wrong type for this operation
397  */
398  class TypeError;
399 
400  /**
401  * @class gum::WrongClassElement agrum/tools/core/exceptions.h
402  * @extends gum::FactoryError
403  * Exception: wrong PRMClassElement for this operation
404  */
405  class WrongClassElement;
406 
407  /**
408  * @class gum::PRMTypeError agrum/tools/core/exceptions.h
409  * @extends gum::FactoryError
410  * Exception : wrong subtype or subclass
411  */
412  class PRMTypeError;
413 
414  /**
415  * @class gum::LearningError agrum/tools/core/exceptions.h
416  * @extends gum::Exception
417  * Exceptions for learning
418  */
419  class LearningError;
420 
421  /**
422  * @class gum::IncompatibleScoreApriori agrum/tools/core/exceptions.h
423  * @extends gum::LearningError
424  * Error: The score already contains a different 'implicit' apriori.
425  */
426  class IncompatibleScoreApriori;
427 
428  /**
429  * @class gum::PossiblyIncompatibleScoreApriori agrum/tools/core/exceptions.h
430  * @extends gum::LearningError
431  * Error: Due to its weight, the apriori is currently compatible with the
432  * score but if you change the weight, it will become incompatible"
433  */
434  class PossiblyIncompatibleScoreApriori;
435 
436  /**
437  * @class gum::DatabaseError agrum/tools/core/exceptions.h
438  * @extends gum::LearningError
439  * Error: An unknown error occurred while accessing a database
440  */
441  class DatabaseError;
442 
443  /**
444  * @class gum::MissingVariableInDatabase agrum/tools/core/exceptions.h
445  * @extends gum::LearningError
446  * Error: A name of variable is not found in the database.
447  */
448  class MissingVariableInDatabase;
449 
450  /**
451  * @class gum::UnknownLabelInDatabase agrum/tools/core/exceptions.h
452  * @extends gum::LearningError
453  * Error: An unknown label is found in the database
454  */
455  class UnknownLabelInDatabase;
456 
457  /**
458  * @class gum::MissingValueInDatabase agrum/tools/core/exceptions.h
459  * @extends gum::LearningError
460  * Error: The database contains some missing values
461  */
462  class MissingValueInDatabase;
463 
464 
465  /**
466  * @class gum::SyntaxError
467  * @headerfile exceptions.h <agrum/tools/core/exceptions.h>
468  * @extends gum::IOError
469  * Special exception for syntax errors in files.
470  */
471  class SyntaxError;
472 
473  /**
474  * @class gum::NotImplementedYet agrum/tools/core/exceptions.h
475  * @extends gum::Exception
476  * Exception : there is something wrong with an arc
477  */
478  class NotImplementedYet;
479 
480 
481 #ifndef DOXYGEN_SHOULD_SKIP_THIS
482  const std::string _createMsg_(const std::string& filename,
483  const std::string& function,
484  int line,
485  const std::string& msg);
486  GUM_MAKE_ERROR(IdError, Exception, "ID error")
487  GUM_MAKE_ERROR(FatalError, Exception, "Fatal error")
488  GUM_MAKE_ERROR(NotImplementedYet, Exception, "Not implemented yet")
489  GUM_MAKE_ERROR(UndefinedIteratorValue, Exception, "Undefined iterator")
490  GUM_MAKE_ERROR(UndefinedIteratorKey, Exception, "Undefined iterator's key")
491  GUM_MAKE_ERROR(NullElement, Exception, "Null element")
492  GUM_MAKE_ERROR(UndefinedElement, Exception, "Undefined element")
493  GUM_MAKE_ERROR(SizeError, Exception, "incorrect size")
494  GUM_MAKE_ERROR(EmptySet, Exception, "Empty set")
495  GUM_MAKE_ERROR(InvalidArgumentsNumber, Exception, "Invalid argument number")
496  GUM_MAKE_ERROR(InvalidArgument, Exception, "Invalid argument")
497  GUM_MAKE_ERROR(IOError, Exception, "I/O Error")
498  GUM_MAKE_ERROR(FormatNotFound, IOError, "Format not found")
499  GUM_MAKE_ERROR(OperationNotAllowed, Exception, "Operation not allowed")
500  GUM_MAKE_ERROR(NotFound, Exception, "Object not found")
501  GUM_MAKE_ERROR(ReferenceError, Exception, "Reference error")
502  GUM_MAKE_ERROR(OutOfBounds, ReferenceError, "Out of bound error")
503  GUM_MAKE_ERROR(OutOfLowerBound, OutOfBounds, "Out of lower bound error")
504  GUM_MAKE_ERROR(OutOfUpperBound, OutOfBounds, "Out of upper bound error")
505  GUM_MAKE_ERROR(DuplicateElement, ReferenceError, "Duplicate element")
506  GUM_MAKE_ERROR(DuplicateLabel, ReferenceError, "Duplicate label")
507  GUM_MAKE_ERROR(GraphError, Exception, "Graph error")
508  GUM_MAKE_ERROR(NoNeighbour, GraphError, "No neighbour found")
509  GUM_MAKE_ERROR(NoParent, GraphError, "No parent found")
510  GUM_MAKE_ERROR(NoChild, GraphError, "No child found")
511  GUM_MAKE_ERROR(InvalidEdge, GraphError, "Invalid edge")
512  GUM_MAKE_ERROR(InvalidArc, GraphError, "Invalid arc")
513  GUM_MAKE_ERROR(InvalidNode, GraphError, "Invalid node")
514  GUM_MAKE_ERROR(EmptyBSTree, GraphError, "Empty binary search tree")
515  GUM_MAKE_ERROR(DefaultInLabel, GraphError, "Error on label")
516  GUM_MAKE_ERROR(InvalidDirectedCycle, GraphError, "Directed cycle detected")
517  GUM_MAKE_ERROR(CPTError, Exception, "CPT error")
518  GUM_MAKE_ERROR(CPTNoSumTo1, CPTError, "CPT does not sum to 1")
519  GUM_MAKE_ERROR(IncompatibleEvidence,
520  Exception,
521  "Several evidence/CPT are incompatible together (proba=0)")
522  GUM_MAKE_ERROR(FactoryError, Exception, "Factory error")
523  GUM_MAKE_ERROR(FactoryInvalidState, FactoryError, "Invalid state error")
524  GUM_MAKE_ERROR(TypeError, FactoryError, "Wrong type")
525  GUM_MAKE_ERROR(WrongClassElement, FactoryError, "Wrong ClassElement")
526  GUM_MAKE_ERROR(PRMTypeError, FactoryError, "Wrong subtype or subclass")
527  GUM_MAKE_ERROR(LearningError, Exception, "Factory error")
528  GUM_MAKE_ERROR(IncompatibleScoreApriori, LearningError, "Incompatible (maybe implicit) priors")
529  GUM_MAKE_ERROR(PossiblyIncompatibleScoreApriori,
530  LearningError,
531  "Possible incompatibility between score and prior")
532  GUM_MAKE_ERROR(DatabaseError, LearningError, "Database error")
533  GUM_MAKE_ERROR(MissingVariableInDatabase, LearningError, "Missing variable name in database")
534  GUM_MAKE_ERROR(MissingValueInDatabase, LearningError, "The database contains some missing values")
535  GUM_MAKE_ERROR(UnknownLabelInDatabase, LearningError, "Unknown label found in database")
536 
537  class SyntaxError: public IOError {
538  protected:
539  Size noLine_;
540  Size noCol_;
541 
542  public:
543  SyntaxError(const std::string& aMsg,
544  Size nol,
545  Size noc,
546  const std::string& aType = "Syntax Error") :
547  IOError(aMsg, aType),
548  noLine_(nol), noCol_(noc){
549 
550  };
551 
552  Size col() const { return noCol_; };
553  Size line() const { return noLine_; };
554  };
555 #endif // DOXYGEN_SHOULD_SKIP_THIS
556 } /* namespace gum */
557 
558 #endif /* GUM_EXCEPTIONS_H */
#define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG)
Definition: exceptions.h:82