Flow123d  JS_before_hm-2115-gf629a871a
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  virtual ~Exception() throw () {};
38 protected:
39  std::ostringstream &form_message(std::ostringstream &) const override;
40 };
41 
42 
43 /**
44  * @brief Declaration of error info class for passing Input::Address through exceptions.
45  *
46  * Is returned by input accessors : Input::Record, Input::Array, etc.
47  *
48  * Use case example:
49  * Input::Record input = ...;
50  * string name=input.val("name");
51  * if (name.size() > STR_LIMIT) THROW(ExcToLongStr() << EI_Address( input.address_string() ));
52  *
53  * TODO: if Address class is persistent (every copy is self contented, we can use Address instead of std::string.
54  * see also ei_address methods.
55  */
56 TYPEDEF_ERR_INFO( EI_Address, const std::string);
57 
58 
59 /**
60  * @brief Macro for simple definition of input exceptions.
61  *
62  * Works in the same way as @p DECLARE_EXCEPTION, just define class derived from
63  * @p InputException. Meant to be used for exceptions due to wrong input from user.
64  *
65  * Reports input address provided through EI_Address object, see above.
66  *
67  * @ingroup exceptions
68  */
69 #define DECLARE_INPUT_EXCEPTION( ExcName, Format) \
70 struct ExcName : public virtual ::Input::Exception { \
71  virtual void print_info(std::ostringstream &out) const { \
72  using namespace internal; \
73  ::internal::ExcStream estream(out, *this); \
74  estream Format \
75  << "\nAt input address: " \
76  << ::Input::EI_Address::val; \
77  out << std::endl; \
78  } \
79  virtual ~ExcName() throw () {} \
80 }
81 
82 /// Simple input exception that accepts just string message.
83 DECLARE_INPUT_EXCEPTION(ExcInputMessage, << EI_Message::val );
84 
85 
86 } // namespace Input
87 
88 #endif /* SRC_INPUT_EXCEPTION_HH_ */
89 
ExceptionBase
Base of exceptions used in Flow123d.
Definition: exceptions.hh:75
Input
Abstract linear system class.
Definition: balance.hh:40
Input::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_InputType, const string)
Input::Exception::~Exception
virtual ~Exception()
Definition: input_exception.hh:37
exceptions.hh
Input::Exception::form_message
std::ostringstream & form_message(std::ostringstream &) const override
Definition: accessors.cc:31
exc_common.hh
Input::Exception
Base of exceptions due to user input.
Definition: input_exception.hh:34
Input::DECLARE_INPUT_EXCEPTION
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.