1 //
2 // Exception.cpp
3 //
4 // Library: Foundation
5 // Package: Core
6 // Module: Exception
7 //
8 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9 // and Contributors.
10 //
11 // SPDX-License-Identifier: BSL-1.0
12 //
13
14
15 #include "Poco/Exception.h"
16 #include <typeinfo>
17
18
19 namespace Poco {
20
21
Exception(int code)22 Exception::Exception(int code): _pNested(0), _code(code)
23 {
24 }
25
26
Exception(const std::string & msg,int code)27 Exception::Exception(const std::string& msg, int code): _msg(msg), _pNested(0), _code(code)
28 {
29 }
30
31
Exception(const std::string & msg,const std::string & arg,int code)32 Exception::Exception(const std::string& msg, const std::string& arg, int code): _msg(msg), _pNested(0), _code(code)
33 {
34 if (!arg.empty())
35 {
36 _msg.append(": ");
37 _msg.append(arg);
38 }
39 }
40
41
Exception(const std::string & msg,const Exception & nested,int code)42 Exception::Exception(const std::string& msg, const Exception& nested, int code): _msg(msg), _pNested(nested.clone()), _code(code)
43 {
44 }
45
46
Exception(const Exception & exc)47 Exception::Exception(const Exception& exc):
48 std::exception(exc),
49 _msg(exc._msg),
50 _code(exc._code)
51 {
52 _pNested = exc._pNested ? exc._pNested->clone() : 0;
53 }
54
55
~Exception()56 Exception::~Exception() noexcept
57 {
58 delete _pNested;
59 }
60
61
operator =(const Exception & exc)62 Exception& Exception::operator = (const Exception& exc)
63 {
64 if (&exc != this)
65 {
66 Exception* newPNested = exc._pNested ? exc._pNested->clone() : 0;
67 delete _pNested;
68 _msg = exc._msg;
69 _pNested = newPNested;
70 _code = exc._code;
71 }
72 return *this;
73 }
74
75
name() const76 const char* Exception::name() const noexcept
77 {
78 return "Exception";
79 }
80
81
className() const82 const char* Exception::className() const noexcept
83 {
84 return typeid(*this).name();
85 }
86
87
what() const88 const char* Exception::what() const noexcept
89 {
90 return name();
91 }
92
93
displayText() const94 std::string Exception::displayText() const
95 {
96 std::string txt = name();
97 if (!_msg.empty())
98 {
99 txt.append(": ");
100 txt.append(_msg);
101 }
102 return txt;
103 }
104
105
extendedMessage(const std::string & arg)106 void Exception::extendedMessage(const std::string& arg)
107 {
108 if (!arg.empty())
109 {
110 if (!_msg.empty()) _msg.append(": ");
111 _msg.append(arg);
112 }
113 }
114
115
clone() const116 Exception* Exception::clone() const
117 {
118 return new Exception(*this);
119 }
120
121
rethrow() const122 void Exception::rethrow() const
123 {
124 throw *this;
125 }
126
127
128 POCO_IMPLEMENT_EXCEPTION(LogicException, Exception, "Logic exception")
129 POCO_IMPLEMENT_EXCEPTION(AssertionViolationException, LogicException, "Assertion violation")
130 POCO_IMPLEMENT_EXCEPTION(NullPointerException, LogicException, "Null pointer")
131 POCO_IMPLEMENT_EXCEPTION(NullValueException, LogicException, "Null value")
132 POCO_IMPLEMENT_EXCEPTION(BugcheckException, LogicException, "Bugcheck")
133 POCO_IMPLEMENT_EXCEPTION(InvalidArgumentException, LogicException, "Invalid argument")
134 POCO_IMPLEMENT_EXCEPTION(NotImplementedException, LogicException, "Not implemented")
135 POCO_IMPLEMENT_EXCEPTION(RangeException, LogicException, "Out of range")
136 POCO_IMPLEMENT_EXCEPTION(IllegalStateException, LogicException, "Illegal state")
137 POCO_IMPLEMENT_EXCEPTION(InvalidAccessException, LogicException, "Invalid access")
138 POCO_IMPLEMENT_EXCEPTION(SignalException, LogicException, "Signal received")
139 POCO_IMPLEMENT_EXCEPTION(UnhandledException, LogicException, "Unhandled exception")
140
141 POCO_IMPLEMENT_EXCEPTION(RuntimeException, Exception, "Runtime exception")
142 POCO_IMPLEMENT_EXCEPTION(NotFoundException, RuntimeException, "Not found")
143 POCO_IMPLEMENT_EXCEPTION(ExistsException, RuntimeException, "Exists")
144 POCO_IMPLEMENT_EXCEPTION(TimeoutException, RuntimeException, "Timeout")
145 POCO_IMPLEMENT_EXCEPTION(SystemException, RuntimeException, "System exception")
146 POCO_IMPLEMENT_EXCEPTION(RegularExpressionException, RuntimeException, "Error in regular expression")
147 POCO_IMPLEMENT_EXCEPTION(LibraryLoadException, RuntimeException, "Cannot load library")
148 POCO_IMPLEMENT_EXCEPTION(LibraryAlreadyLoadedException, RuntimeException, "Library already loaded")
149 POCO_IMPLEMENT_EXCEPTION(NoThreadAvailableException, RuntimeException, "No thread available")
150 POCO_IMPLEMENT_EXCEPTION(PropertyNotSupportedException, RuntimeException, "Property not supported")
151 POCO_IMPLEMENT_EXCEPTION(PoolOverflowException, RuntimeException, "Pool overflow")
152 POCO_IMPLEMENT_EXCEPTION(NoPermissionException, RuntimeException, "No permission")
153 POCO_IMPLEMENT_EXCEPTION(OutOfMemoryException, RuntimeException, "Out of memory")
154 POCO_IMPLEMENT_EXCEPTION(DataException, RuntimeException, "Data error")
155
156 POCO_IMPLEMENT_EXCEPTION(DataFormatException, DataException, "Bad data format")
157 POCO_IMPLEMENT_EXCEPTION(SyntaxException, DataException, "Syntax error")
158 POCO_IMPLEMENT_EXCEPTION(CircularReferenceException, DataException, "Circular reference")
159 POCO_IMPLEMENT_EXCEPTION(PathSyntaxException, SyntaxException, "Bad path syntax")
160 POCO_IMPLEMENT_EXCEPTION(IOException, RuntimeException, "I/O error")
161 POCO_IMPLEMENT_EXCEPTION(ProtocolException, IOException, "Protocol error")
162 POCO_IMPLEMENT_EXCEPTION(FileException, IOException, "File access error")
163 POCO_IMPLEMENT_EXCEPTION(FileExistsException, FileException, "File exists")
164 POCO_IMPLEMENT_EXCEPTION(FileNotFoundException, FileException, "File not found")
165 POCO_IMPLEMENT_EXCEPTION(PathNotFoundException, FileException, "Path not found")
166 POCO_IMPLEMENT_EXCEPTION(FileReadOnlyException, FileException, "File is read-only")
167 POCO_IMPLEMENT_EXCEPTION(FileAccessDeniedException, FileException, "Access to file denied")
168 POCO_IMPLEMENT_EXCEPTION(CreateFileException, FileException, "Cannot create file")
169 POCO_IMPLEMENT_EXCEPTION(OpenFileException, FileException, "Cannot open file")
170 POCO_IMPLEMENT_EXCEPTION(WriteFileException, FileException, "Cannot write file")
171 POCO_IMPLEMENT_EXCEPTION(ReadFileException, FileException, "Cannot read file")
172 POCO_IMPLEMENT_EXCEPTION(DirectoryNotEmptyException, FileException, "Directory not empty")
173 POCO_IMPLEMENT_EXCEPTION(UnknownURISchemeException, RuntimeException, "Unknown URI scheme")
174 POCO_IMPLEMENT_EXCEPTION(TooManyURIRedirectsException, RuntimeException, "Too many URI redirects")
175 POCO_IMPLEMENT_EXCEPTION(URISyntaxException, SyntaxException, "Bad URI syntax")
176
177 POCO_IMPLEMENT_EXCEPTION(ApplicationException, Exception, "Application exception")
178 POCO_IMPLEMENT_EXCEPTION(BadCastException, RuntimeException, "Bad cast exception")
179
180
181 } // namespace Poco
182