libexcept 1.1.19
Stack trace along C++ exceptions
exception.h
Go to the documentation of this file.
1// Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/libexcept
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program 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 General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19#pragma once
20
21// self
22//
24
25
26// C++ includes
27//
28#include <map>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33
44namespace libexcept
45{
46
47
48
50{
51 COLLECT_STACK_NO, // no stack trace for exceptions
52 COLLECT_STACK_YES, // plain stack trace (fast)
53 COLLECT_STACK_COMPLETE, // include filenames & line numbers (slow)
54};
55
56
57typedef std::map<std::string, std::string> parameter_t;
58
59
61void set_collect_stack(collect_stack_t collect_stack);
62
63
65{
66public:
67 explicit exception_base_t(int const stack_trace_depth = STACK_TRACE_DEPTH);
68
69 virtual ~exception_base_t() {}
70
71 parameter_t const & get_parameters() const;
72 std::string get_parameter(std::string const & name) const;
73 exception_base_t & set_parameter(std::string const & name, std::string const & value);
74
75 stack_trace_t const & get_stack_trace() const { return f_stack_trace; }
76
77private:
80};
81
82
84 : public std::logic_error
85 , public exception_base_t
86{
87public:
88 explicit logic_exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
89 explicit logic_exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
90
91 virtual ~logic_exception_t() override {}
92
93 virtual char const * what() const throw() override;
94};
95
96
98 : public std::out_of_range
99 , public exception_base_t
100{
101public:
102 explicit out_of_range_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
103 explicit out_of_range_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
104
105 virtual ~out_of_range_t() override {}
106
107 virtual char const * what() const throw() override;
108};
109
110
112 : public std::runtime_error
113 , public exception_base_t
114{
115public:
116 explicit exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
117 explicit exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
118
119 virtual ~exception_t() override {}
120
121 virtual char const * what() const throw() override;
122};
123
124
125#define DECLARE_LOGIC_ERROR(name) \
126 class name : public ::libexcept::logic_exception_t { \
127 public: name(std::string const & msg) : logic_exception_t(#name ": " + msg) {} }
128
129#define DECLARE_OUT_OF_RANGE(name) \
130 class name : public ::libexcept::out_of_range_t { \
131 public: name(std::string const & msg) : out_of_range_t(#name ": " + msg) {} }
132
133#define DECLARE_MAIN_EXCEPTION(name) \
134 class name : public ::libexcept::exception_t { \
135 public: name(std::string const & msg) : exception_t(#name ": " + msg) {} }
136
137#define DECLARE_EXCEPTION(base, name) \
138 class name : public base { \
139 public: name(std::string const & msg) : base(msg) {} }
140
141
142// a default logic error where I know there is a problem that needs to be
143// fixed so that way we can quickly find the location & read the comments
144//
146
147
148}
149// namespace libexcept
150// vim: ts=4 sw=4 et
exception_base_t & set_parameter(std::string const &name, std::string const &value)
Set a parameter in this exception.
parameter_t const & get_parameters() const
Retrieve the set of exception parameters.
std::string get_parameter(std::string const &name) const
Retrieve one of the exception parameters.
stack_trace_t f_stack_trace
The variable where the exception stack trace gets saved.
Definition exception.h:79
stack_trace_t const & get_stack_trace() const
Retrieve the stack trace.
Definition exception.h:75
virtual ~exception_base_t()
Destructor of the exception base class.
Definition exception.h:69
virtual ~exception_t() override
Destructor of the exception class.
Definition exception.h:119
virtual ~logic_exception_t() override
Destructor of the logic exception class.
Definition exception.h:91
virtual char const * what() const override
Retrieve the what parameter as passed to the constructor.
virtual ~out_of_range_t() override
Destructor of the out_of_range exception class.
Definition exception.h:105
#define DECLARE_LOGIC_ERROR(name)
Definition exception.h:125
void set_collect_stack(collect_stack_t collect_stack)
Set a general flag on whether to collect stack traces or not.
constexpr int const STACK_TRACE_DEPTH
Default depth of stack traces collected.
Definition stack_trace.h:39
std::map< std::string, std::string > parameter_t
Definition exception.h:57
collect_stack_t get_collect_stack()
Tells you whether the general flag is true or false.
std::list< std::string > stack_trace_t
The stack trace results.
Definition stack_trace.h:41
Declarations of the stack trace functions.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.