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