aGrUM  0.21.0
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  /**
147  * @class gum::FatalError agrum/tools/core/exceptions.h
148  * @extends gum::Exception
149  * Exception : fatal (unknown ?) error
150  */
151  class FatalError;
152 
153  /**
154  * @class gum::UndefinedIteratorValue agrum/tools/core/exceptions.h
155  * @extends gum::Exception
156  * Exception : generic error on iterator
157  */
158  class IteratorError;
159 
160  /**
161  * @class gum::UndefinedIteratorValue agrum/tools/core/exceptions.h
162  * @extends gum::Exception
163  * Exception : iterator does not point to any valid value
164  */
165  class UndefinedIteratorValue;
166 
167  /**
168  * @class gum::UndefinedIteratorKey agrum/tools/core/exceptions.h
169  * @extends gum::Exception
170  * Exception : iterator does not point to any valid key
171  */
172  class UndefinedIteratorKey;
173 
174  /**
175  * @class gum::NullElement agrum/tools/core/exceptions.h
176  * @extends gum::Exception
177  * Exception : a pointer or a reference on a nullptr (0) object
178  */
179  class NullElement;
180 
181  /**
182  * @class gum::UndefinedElement agrum/tools/core/exceptions.h
183  * @extends gum::Exception
184  * Exception : a looked-for element could not be found
185  */
186  class UndefinedElement;
187 
188  /**
189  * @class gum::SizeError agrum/tools/core/exceptions.h
190  * @extends gum::Exception
191  * Exception : problem with size
192  */
193  class SizeError;
194 
195 
196  /**
197  * @class gum::InvalidArgumentsNumber agrum/tools/core/exceptions.h
198  * @extends gum::Exception
199  * Exception: the number of arguments passed to a function is not what was
200  * expected
201  */
202  class InvalidArgumentsNumber;
203 
204  /**
205  * @class gum::InvalidArgument agrum/tools/core/exceptions.h
206  * @extends gum::Exception
207  * Exception: at least one argument passed to a function is not what was
208  * expected
209  */
210  class InvalidArgument;
211 
212  /**
213  * @class gum::IOError agrum/tools/core/exceptions.h
214  * @extends gum::Exception
215  * Exception : input/output problem
216  */
217  class IOError;
218 
219  /**
220  * @class gum::FormatNotFound agrum/tools/core/exceptions.h
221  * @extends gum::Exception
222  * Exception : a I/O format was not found
223  */
224  class FormatNotFound;
225 
226  /**
227  * @class gum::OperationNotAllowed agrum/tools/core/exceptions.h
228  * @extends gum::Exception
229  * Exception : operation not allowed
230  */
231  class OperationNotAllowed;
232 
233  /**
234  * @class gum::NotFound agrum/tools/core/exceptions.h
235  * @extends gum::Exception
236  * Exception : the element we looked for cannot be found
237  */
238  class NotFound;
239 
240  /**
241  * @class gum::ArgumentError agrum/tools/core/exceptions.h
242  * @extends gum::Exception
243  * Exception base for argument error
244  */
245  class ArgumentError;
246 
247  /**
248  * @class gum::OutOfBounds agrum/tools/core/exceptions.h
249  * @extends gum::ArgumentError
250  * Exception : out of bound
251  */
252  class OutOfBounds;
253 
254  /**
255  * @class gum::DuplicateElement agrum/tools/core/exceptions.h
256  * @extends gum::ArgumentError
257  * Exception : a similar element already exists
258  */
259  class DuplicateElement;
260 
261  /**
262  * @class gum::DuplicateLabel agrum/tools/core/exceptions.h
263  * @extends gum::ArgumentError
264  * Exception : a similar label already exists
265  */
266  class DuplicateLabel;
267 
268  ///////////////////////////////////
269  /**
270  * @class gum::GraphError agrum/tools/core/exceptions.h
271  * @extends gum::Exception
272  * Exception base for graph error
273  */
274  class GraphError;
275 
276  /**
277  * @class gum::NoNeighbour agrum/tools/core/exceptions.h
278  * @extends gum::GraphError
279  * Exception : no neighbour to a given node was found
280  */
281  class NoNeighbour;
282 
283  /**
284  * @class gum::NoParent agrum/tools/core/exceptions.h
285  * @extends gum::GraphError
286  * Exception : no parent for a given node was found
287  */
288  class NoParent;
289 
290  /**
291  * @class gum::NoChild agrum/tools/core/exceptions.h
292  * @extends gum::GraphError
293  * Exception : no child for a given node was found
294  */
295  class NoChild;
296 
297  /**
298  * @class gum::InvalidEdge agrum/tools/core/exceptions.h
299  * @extends gum::GraphError
300  * Exception : there is something wrong with an edge
301  */
302  class InvalidEdge;
303 
304  /**
305  * @class gum::InvalidArc agrum/tools/core/exceptions.h
306  * @extends gum::GraphError
307  * Exception : there is something wrong with an arc
308  */
309  class InvalidArc;
310 
311  /**
312  * @class gum::InvalidNode agrum/tools/core/exceptions.h
313  * @extends gum::GraphError
314  * Exception : node does not exist
315  */
316  class InvalidNode;
317 
318  /**
319  * @class gum::DefaultInLabel agrum/tools/core/exceptions.h
320  * @extends gum::GraphError
321  * Exception : default in label
322  */
323  class DefaultInLabel;
324 
325  /**
326  * @class gum::InvalidDirectedCycle agrum/tools/core/exceptions.h
327  * @extends gum::GraphError
328  * Exception : existence of a directed cycle in a graph
329  */
330  class InvalidDirectedCycle;
331 
332  ///////////////////////////////////
333  /**
334  * @class gum::CPTError agrum/tools/core/exceptions.h
335  * @extends gum::Exception
336  * Exception base for CPT error
337  */
338  class CPTError;
339 
340 
341  /**
342  * @class gum::IncompatibleEvidence agrum/tools/core/exceptions.h
343  * Exception : several evidence are incompatible together (proba=0)
344  */
345  class IncompatibleEvidence;
346 
347  /**
348  * @class gum::FactoryError agrum/tools/core/exceptions.h
349  * @extends gum::Exception
350  * Exception base for factory error
351  */
352  class FactoryError;
353 
354  /**
355  * @class gum::FactoryInvalidState agrum/tools/core/exceptions.h
356  * @extends gum::FactoryError
357  * Exception : invalid state error
358  */
359  class FactoryInvalidState;
360 
361  /**
362  * @class gum::TypeError agrum/tools/core/exceptions.h
363  * @extends gum::FactoryError
364  * Exception : wrong type for this operation
365  */
366  class TypeError;
367 
368  /**
369  * @class gum::WrongClassElement agrum/tools/core/exceptions.h
370  * @extends gum::FactoryError
371  * Exception: wrong PRMClassElement for this operation
372  */
373  class WrongClassElement;
374 
375  /**
376  * @class gum::PRMTypeError agrum/tools/core/exceptions.h
377  * @extends gum::FactoryError
378  * Exception : wrong subtype or subclass
379  */
380  class PRMTypeError;
381 
382  /**
383  * @class gum::LearningError agrum/tools/core/exceptions.h
384  * @extends gum::Exception
385  * Exceptions for learning
386  */
387  class LearningError;
388 
389  /**
390  * @class gum::IncompatibleScoreApriori agrum/tools/core/exceptions.h
391  * @extends gum::LearningError
392  * Error: The score already contains a different 'implicit' apriori.
393  */
394  class IncompatibleScoreApriori;
395 
396  /**
397  * @class gum::PossiblyIncompatibleScoreApriori agrum/tools/core/exceptions.h
398  * @extends gum::LearningError
399  * Error: Due to its weight, the apriori is currently compatible with the
400  * score but if you change the weight, it will become incompatible"
401  */
402  class PossiblyIncompatibleScoreApriori;
403 
404  /**
405  * @class gum::DatabaseError agrum/tools/core/exceptions.h
406  * @extends gum::LearningError
407  * Error: An unknown error occurred while accessing a database
408  */
409  class DatabaseError;
410 
411  /**
412  * @class gum::MissingVariableInDatabase agrum/tools/core/exceptions.h
413  * @extends gum::LearningError
414  * Error: A name of variable is not found in the database.
415  */
416  class MissingVariableInDatabase;
417 
418  /**
419  * @class gum::UnknownLabelInDatabase agrum/tools/core/exceptions.h
420  * @extends gum::LearningError
421  * Error: An unknown label is found in the database
422  */
423  class UnknownLabelInDatabase;
424 
425  /**
426  * @class gum::MissingValueInDatabase agrum/tools/core/exceptions.h
427  * @extends gum::LearningError
428  * Error: The database contains some missing values
429  */
430  class MissingValueInDatabase;
431 
432 
433  /**
434  * @class gum::SyntaxError
435  * @headerfile exceptions.h <agrum/tools/core/exceptions.h>
436  * @extends gum::IOError
437  * Special exception for syntax errors in files.
438  */
439  class SyntaxError;
440 
441  /**
442  * @class gum::NotImplementedYet agrum/tools/core/exceptions.h
443  * @extends gum::Exception
444  * Exception : there is something wrong with an arc
445  */
446  class NotImplementedYet;
447 
448 
449 #ifndef DOXYGEN_SHOULD_SKIP_THIS
450  const std::string _createMsg_(const std::string& filename,
451  const std::string& function,
452  int line,
453  const std::string& msg);
454  GUM_MAKE_ERROR(FatalError, Exception, "Fatal error")
455  GUM_MAKE_ERROR(NotImplementedYet, Exception, "Not implemented yet")
456  GUM_MAKE_ERROR(IteratorError, Exception, "Error in iterator")
457  GUM_MAKE_ERROR(UndefinedIteratorValue, IteratorError, "Undefined iterator")
458  GUM_MAKE_ERROR(UndefinedIteratorKey, IteratorError, "Undefined iterator's key")
459  GUM_MAKE_ERROR(NullElement, Exception, "Null element")
460  GUM_MAKE_ERROR(UndefinedElement, Exception, "Undefined element")
461  GUM_MAKE_ERROR(SizeError, Exception, "incorrect size")
462  GUM_MAKE_ERROR(ArgumentError, Exception, "Argument error")
463  GUM_MAKE_ERROR(InvalidArgumentsNumber, ArgumentError, "Invalid argument number")
464  GUM_MAKE_ERROR(InvalidArgument, ArgumentError, "Invalid argument")
465  GUM_MAKE_ERROR(IOError, Exception, "I/O Error")
466  GUM_MAKE_ERROR(FormatNotFound, IOError, "Format not found")
467  GUM_MAKE_ERROR(OperationNotAllowed, Exception, "Operation not allowed")
468  GUM_MAKE_ERROR(NotFound, Exception, "Object not found")
469  GUM_MAKE_ERROR(OutOfBounds, ArgumentError, "Out of bound error")
470  GUM_MAKE_ERROR(DuplicateElement, ArgumentError, "Duplicate element")
471  GUM_MAKE_ERROR(DuplicateLabel, ArgumentError, "Duplicate label")
472  GUM_MAKE_ERROR(GraphError, Exception, "Graph error")
473  GUM_MAKE_ERROR(NoNeighbour, GraphError, "No neighbour found")
474  GUM_MAKE_ERROR(NoParent, GraphError, "No parent found")
475  GUM_MAKE_ERROR(NoChild, GraphError, "No child found")
476  GUM_MAKE_ERROR(InvalidEdge, GraphError, "Invalid edge")
477  GUM_MAKE_ERROR(InvalidArc, GraphError, "Invalid arc")
478  GUM_MAKE_ERROR(InvalidNode, GraphError, "Invalid node")
479  GUM_MAKE_ERROR(DefaultInLabel, GraphError, "Error on label")
480  GUM_MAKE_ERROR(InvalidDirectedCycle, GraphError, "Directed cycle detected")
481  GUM_MAKE_ERROR(CPTError, Exception, "CPT error")
482  GUM_MAKE_ERROR(IncompatibleEvidence,
483  Exception,
484  "Several evidence/CPT are incompatible together (proba=0)")
485  GUM_MAKE_ERROR(FactoryError, Exception, "Factory error")
486  GUM_MAKE_ERROR(FactoryInvalidState, FactoryError, "Invalid state error")
487  GUM_MAKE_ERROR(TypeError, FactoryError, "Wrong type")
488  GUM_MAKE_ERROR(WrongClassElement, FactoryError, "Wrong ClassElement")
489  GUM_MAKE_ERROR(PRMTypeError, FactoryError, "Wrong subtype or subclass")
490  GUM_MAKE_ERROR(LearningError, Exception, "Factory error")
491  GUM_MAKE_ERROR(IncompatibleScoreApriori, LearningError, "Incompatible (maybe implicit) priors")
492  GUM_MAKE_ERROR(PossiblyIncompatibleScoreApriori,
493  LearningError,
494  "Possible incompatibility between score and prior")
495  GUM_MAKE_ERROR(DatabaseError, LearningError, "Database error")
496  GUM_MAKE_ERROR(MissingVariableInDatabase, LearningError, "Missing variable name in database")
497  GUM_MAKE_ERROR(MissingValueInDatabase, LearningError, "The database contains some missing values")
498  GUM_MAKE_ERROR(UnknownLabelInDatabase, LearningError, "Unknown label found in database")
499 
500  class SyntaxError: public IOError {
501  protected:
502  Size noLine_;
503  Size noCol_;
504 
505  public:
506  SyntaxError(const std::string& aMsg,
507  Size nol,
508  Size noc,
509  const std::string& aType = "Syntax Error") :
510  IOError(aMsg, aType),
511  noLine_(nol), noCol_(noc){
512 
513  };
514 
515  Size col() const { return noCol_; };
516  Size line() const { return noLine_; };
517  };
518 #endif // DOXYGEN_SHOULD_SKIP_THIS
519 } /* namespace gum */
520 
521 #endif /* GUM_EXCEPTIONS_H */
#define GUM_MAKE_ERROR(TYPE, SUPERCLASS, MSG)
Definition: exceptions.h:82