Flow123d  release_1.8.2-1603-g0109a2b
input_exception.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file input_exception.hh
15  * @brief
16  */
17 
18 #ifndef SRC_INPUT_EXCEPTION_HH_
19 #define SRC_INPUT_EXCEPTION_HH_
20 
21 #include "system/exceptions.hh"
22 #include "system/exc_common.hh"
23 
24 namespace Input {
25 
26 /**
27  * @brief Base of exceptions due to user input.
28  *
29  * Base class for "input exceptions" that are exceptions caused by incorrect input from the user
30  * not by an internal error.
31  *
32  * @ingroup exceptions
33  */
34 class Exception : public virtual ExceptionBase
35 {
36 public:
37  const char * what () const throw ();
38  virtual ~Exception() throw () {};
39 };
40 
41 
42 /**
43  * @brief Declaration of error info class for passing Input::Address through exceptions.
44  *
45  * Is returned by input accessors : Input::Record, Input::Array, etc.
46  *
47  * Use case example:
48  * Input::Record input = ...;
49  * string name=input.val("name");
50  * if (name.size() > STR_LIMIT) THROW(ExcToLongStr() << EI_Address( input.address_string() ));
51  *
52  * TODO: if Address class is persistent (every copy is self contented, we can use Address instead of std::string.
53  * see also ei_address methods.
54  */
55 TYPEDEF_ERR_INFO( EI_Address, const std::string);
56 
57 
58 /**
59  * @brief Macro for simple definition of input exceptions.
60  *
61  * Works in the same way as @p DECLARE_EXCEPTION, just define class derived from
62  * @p InputException. Meant to be used for exceptions due to wrong input from user.
63  *
64  * Reports input address provided through EI_Address object, see above.
65  *
66  * @ingroup exceptions
67  */
68 #define DECLARE_INPUT_EXCEPTION( ExcName, Format) \
69 struct ExcName : public virtual ::Input::Exception { \
70  virtual void print_info(std::ostringstream &out) const { \
71  using namespace internal; \
72  ::internal::ExcStream estream(out, *this); \
73  estream Format \
74  << "\nAt input address: " \
75  << ::Input::EI_Address::val; \
76  out << std::endl; \
77  } \
78  virtual ~ExcName() throw () {} \
79 }
80 
81 /// Simple input exception that accepts just string message.
82 DECLARE_INPUT_EXCEPTION(ExcInputMessage, << EI_Message::val );
83 
84 
85 } // namespace Input
86 
87 #endif /* SRC_INPUT_EXCEPTION_HH_ */
88 
const char * what() const
Definition: accessors.cc:32
TYPEDEF_ERR_INFO(EI_InputType, const string)
Base of exceptions due to user input.
Base of exceptions used in Flow123d.
Definition: exceptions.hh:66
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.