Exception.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/Exception.h
22 
23  \brief This class is designed to declare objects to be thrown as exceptions by TerraLib.
24 */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_EXCEPTION_H
27 #define __TERRALIB_COMMON_INTERNAL_EXCEPTION_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "Enums.h"
32 
33 // STL
34 #include <exception>
35 #include <string>
36 
37 namespace te
38 {
39  namespace common
40  {
41  /*!
42  \class Exception
43 
44  \brief This class is designed to declare objects to be thrown as exceptions by TerraLib.
45 
46  What is an Exception?
47  <ul>
48  <li>It can be an error during a processing function: like out-of-memory, wrong path, wrong calculus;</li>
49  <li>It can be triggered by a missed value or pointer;</li>
50  <li>It can be triggered by a parameter out of range.</li>
51  </ul>
52  In other words, it can be any exception thrown by TerraLib when something goes wrong.
53 
54  \note TerraLib Exception class is a subclass of std::exception.
55 
56  \note In future we must revisit this class and remove the embbed std::string.
57  */
58  class TECOMMONEXPORT Exception : public virtual std::exception
59  {
60  public:
61 
62  /*! \brief Default constructor. */
63  Exception() throw();
64 
65  /*!
66  \brief It initializes a new Exception.
67 
68  \param what A brief description of what has raised the exception.
69  */
70  explicit Exception(const std::string& what, int code = UNKNOWN_EXCEPTION) throw();
71 
72  /*!
73  \brief It initializes a new Exception.
74 
75  \param what A brief description of what has raised the exception.
76  */
77  explicit Exception(const char* const what, int code = UNKNOWN_EXCEPTION) throw();
78 
79  /*! \brief Destructor. */
80  virtual ~Exception() throw();
81 
82  /*!
83  \brief It gets the exception code.
84 
85  \return The exception code.
86  */
87  virtual int code() const throw();
88 
89  /*!
90  \brief It outputs the exception message.
91 
92  \return The exception message.
93  */
94  virtual const char* what() const throw();
95 
96  /*!
97  \brief It return the exception class name.
98 
99  \return the exception class name.
100  */
101  virtual const char* getClassName() const throw();
102 
103  protected:
104 
105  int m_code; //!< The internal exception code.
106  std::string m_msg; //!< The internal exception message.
107  };
108 
109  /*!
110  \brief It serializes the exception and sends it to the output stream.
111 
112  \param e An exception object.
113  \param o An output stream.
114 
115  \return The output stream.
116  */
117  std::ostream& operator<<(const Exception& e, std::ostream& o);
118 
119  } // end namespace common
120 } // end namespace te
121 
122 /*!
123  \brief This define can be used to add new exception classes to the system.
124 */
125 #define TE_DECLARE_EXCEPTION_CLASS(API_DECL, ClassName, BaseExceptionClass) \
126  class API_DECL ClassName : public virtual BaseExceptionClass \
127  { \
128  public: \
129  \
130  ClassName() throw() { } \
131  \
132  explicit ClassName(const std::string& what, int code = te::common::UNKNOWN_EXCEPTION) throw(); \
133  \
134  explicit ClassName(const char* const what, int code = te::common::UNKNOWN_EXCEPTION) throw(); \
135  \
136  virtual ~ClassName() throw(); \
137  \
138  virtual const char* getClassName() const throw(); \
139  };
140 
141 /*!
142  \brief This define can be used to add new exception classes to the system.
143 */
144 #define TE_DEFINE_EXCEPTION_CLASS(ClassName, BaseExceptionClass, ClassNameAsLiteral) \
145  ClassName::ClassName(const std::string& what, int code) throw() \
146  : BaseExceptionClass(what, code) \
147  { \
148  m_code = code; \
149  m_msg = what; \
150  } \
151  \
152  ClassName::ClassName(const char* const what, int code) throw() \
153  : BaseExceptionClass(what, code) \
154  { \
155  m_code = code; \
156  m_msg = what; \
157  } \
158  \
159  ClassName::~ClassName() throw() \
160  { \
161  } \
162  \
163  const char* ClassName::getClassName() const throw() \
164  { \
165  return ClassNameAsLiteral; \
166  }
167 
168 #endif // __TERRALIB_COMMON_INTERNAL_EXCEPTION_H
169 
std::ostream & operator<<(const Exception &e, std::ostream &o)
It serializes the exception and sends it to the output stream.
std::string m_msg
The internal exception message.
Definition: Exception.h:106
Configuration flags for the TerraLib Common Runtime module.
int m_code
The internal exception code.
Definition: Exception.h:105
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
General enumerations.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58