lsst.pex.exceptions  15.0-3-g707930d+2
Exception.h
Go to the documentation of this file.
1 /*
2  * This file is part of pex_exceptions.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef LSST_PEX_EXCEPTIONS_EXCEPTION_H
25 #define LSST_PEX_EXCEPTIONS_EXCEPTION_H
26 
27 #include <exception>
28 #include <ostream>
29 #include <string>
30 #include <vector>
31 
32 #include "boost/current_function.hpp"
33 
34 namespace lsst {
35 namespace pex {
36 namespace exceptions {
37 
39 #define LSST_EXCEPT_HERE __FILE__, __LINE__, BOOST_CURRENT_FUNCTION
40 
47 #define LSST_EXCEPT(type, ...) type(LSST_EXCEPT_HERE, __VA_ARGS__)
48 
53 #define LSST_EXCEPT_ADD(e, m) e.addMessage(LSST_EXCEPT_HERE, m)
54 
56 #define LSST_EARGS_TYPED char const *ex_file, int ex_line, char const *ex_func, std::string const &ex_message
57 
59 #define LSST_EARGS_UNTYPED ex_file, ex_line, ex_func, ex_message
60 
68 #define LSST_EXCEPTION_TYPE(t, b, c) \
69  class t : public b { \
70  public: \
71  t(LSST_EARGS_TYPED) : b(LSST_EARGS_UNTYPED){}; \
72  t(std::string const& message) : b(message){}; \
73  virtual char const* getType(void) const noexcept { return #c " *"; }; \
74  virtual lsst::pex::exceptions::Exception* clone(void) const { return new t(*this); }; \
75  };
76 
78 struct Tracepoint {
87  Tracepoint(char const* file, int line, char const* func, std::string const& message);
88 
89  char const* _file; // Compiled strings only; does not need deletion
90  int _line;
91  char const* _func; // Compiled strings only; does not need deletion
93 };
95 
106 class Exception : public std::exception {
107 public:
116  Exception(char const* file, int line, char const* func,
117  std::string const& message); // Should use LSST_EARGS_TYPED, but that confuses doxygen.
118 
129  explicit Exception(std::string const& message);
130 
131  virtual ~Exception(void) noexcept;
132 
141  void addMessage(char const* file, int line, char const* func, std::string const& message);
142 
144  Traceback const& getTraceback(void) const noexcept;
145 
153  virtual std::ostream& addToStream(std::ostream& stream) const;
154 
165  virtual char const* what(void) const noexcept;
166 
174  virtual char const* getType(void) const noexcept;
175 
182  virtual Exception* clone(void) const;
183 
184 private:
186  Traceback _traceback;
187 };
188 
196 std::ostream& operator<<(std::ostream& stream, Exception const& e);
197 }
198 } // namespace pex
199 } // namespace lsst
200 
201 #endif
Provides consistent interface for LSST exceptions.
Definition: Exception.h:106
STL class.
Tracepoint(char const *file, int line, char const *func, std::string const &message)
Standard constructor, intended for C++ use.
Definition: Exception.cc:39
STL class.
std::ostream & operator<<(std::ostream &stream, Exception const &e)
Push the text representation of an exception onto a stream.
Definition: Exception.cc:105
file
STL class.
One point in the Traceback vector held by Exception.
Definition: Exception.h:78
std::vector< Tracepoint > Traceback
Definition: Exception.h:94