Skip to content

Commit ec53ee7

Browse files
committed
Improve exception error messages
* Inherit exceptions from std::runtime_error * Refactor error message functions * Refactor path construction functions * Add path to error messages
1 parent f53e5de commit ec53ee7

File tree

2 files changed

+176
-94
lines changed

2 files changed

+176
-94
lines changed

lib/libconfig.h++

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define __libconfig_hpp
2525

2626
#include <stdio.h>
27-
#include <exception>
27+
#include <stdexcept>
2828
#include <string>
2929

3030
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
@@ -54,14 +54,31 @@ struct config_setting_t; // fwd decl
5454

5555
namespace libconfig {
5656

57-
class LIBCONFIGXX_API ConfigException : public std::exception { };
57+
struct LIBCONFIGXX_API ConfigException : public std::runtime_error
58+
{
59+
ConfigException();
60+
ConfigException(std::string const &message);
61+
62+
ConfigException(ConfigException const &other);
63+
ConfigException& operator=(ConfigException const &other);
64+
65+
virtual ~ConfigException() LIBCONFIGXX_NOEXCEPT
66+
};
5867

5968
class Setting; // fwd decl
6069
class SettingIterator;
6170
class SettingConstIterator;
6271

6372
class LIBCONFIGXX_API SettingException : public ConfigException
6473
{
74+
75+
protected:
76+
77+
SettingException(char const *derivedType, const Setting &setting);
78+
SettingException(char const *derivedType, const Setting &setting, int idx);
79+
SettingException(char const *derivedType, const Setting &setting, const char *name);
80+
SettingException(char const *derivedType, std::string path);
81+
6582
public:
6683

6784
SettingException(const Setting &setting);
@@ -74,13 +91,11 @@ class LIBCONFIGXX_API SettingException : public ConfigException
7491

7592
virtual ~SettingException() LIBCONFIGXX_NOEXCEPT;
7693

77-
const char *getPath() const;
78-
79-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
94+
std::string const & getPath() const;
8095

8196
private:
8297

83-
char *_path;
98+
std::string _path;
8499
};
85100

86101
class LIBCONFIGXX_API SettingTypeException : public SettingException
@@ -90,8 +105,6 @@ class LIBCONFIGXX_API SettingTypeException : public SettingException
90105
SettingTypeException(const Setting &setting);
91106
SettingTypeException(const Setting &setting, int idx);
92107
SettingTypeException(const Setting &setting, const char *name);
93-
94-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
95108
};
96109

97110
class LIBCONFIGXX_API SettingNotFoundException : public SettingException
@@ -101,24 +114,18 @@ class LIBCONFIGXX_API SettingNotFoundException : public SettingException
101114
SettingNotFoundException(const char *path);
102115
SettingNotFoundException(const Setting &setting, int idx);
103116
SettingNotFoundException(const Setting &setting, const char *name);
104-
105-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
106117
};
107118

108119
class LIBCONFIGXX_API SettingNameException : public SettingException
109120
{
110121
public:
111122

112123
SettingNameException(const Setting &setting, const char *name);
113-
114-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
115124
};
116125

117-
class LIBCONFIGXX_API FileIOException : public ConfigException
126+
struct LIBCONFIGXX_API FileIOException : public ConfigException
118127
{
119-
public:
120-
121-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
128+
FileIOException();
122129
};
123130

124131
class LIBCONFIGXX_API ParseException : public ConfigException
@@ -140,8 +147,6 @@ class LIBCONFIGXX_API ParseException : public ConfigException
140147
inline const char *getError() const
141148
{ return(_error); }
142149

143-
virtual const char *what() const LIBCONFIGXX_NOEXCEPT;
144-
145150
private:
146151

147152
const char *_file;

0 commit comments

Comments
 (0)