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 "Poco/NestedDiagnosticContext.h"
17#include <typeinfo>
18
19
20namespace Poco {
21
22
23Exception::Exception(int otherCode): _pNested(0), _code(otherCode)
24{
25 addBacktrace();
26}
27
28
29Exception::Exception(const std::string& msg, int otherCode):
30 _msg(msg),
31 _pNested(0),
32 _code(otherCode)
33{
34 addBacktrace();
35}
36
37
38Exception::Exception(const std::string& msg, const std::string& arg, int otherCode):
39 _msg(msg),
40 _pNested(0),
41 _code(otherCode)
42{
43 if (!arg.empty())
44 {
45 _msg.append(": ");
46 _msg.append(arg);
47 }
48 addBacktrace();
49}
50
51
52Exception::Exception(const std::string& msg, const Exception& nestedException, int otherCode):
53 _msg(msg),
54 _pNested(nestedException.clone()),
55 _code(otherCode)
56{
57 addBacktrace();
58}
59
60
61Exception::Exception(const Exception& exc):
62 std::exception(exc),
63 _msg(exc._msg),
64 _code(exc._code)
65{
66 _pNested = exc._pNested ? exc._pNested->clone() : 0;
67}
68
69
70Exception::~Exception() throw()
71{
72 delete _pNested;
73}
74
75
76Exception& Exception::operator = (const Exception& exc)
77{
78 if (&exc != this)
79 {
80 Exception* newPNested = exc._pNested ? exc._pNested->clone() : 0;
81 delete _pNested;
82 _msg = exc._msg;
83 _pNested = newPNested;
84 _code = exc._code;
85 }
86 return *this;
87}
88
89
90const char* Exception::name() const throw()
91{
92 return "Exception";
93}
94
95
96const char* Exception::className() const throw()
97{
98 return typeid(*this).name();
99}
100
101
102const char* Exception::what() const throw()
103{
104 return msg().c_str();//name();
105}
106
107
108std::string Exception::displayText() const
109{
110 std::string txt;
111 if (!_msg.empty()) txt.append(msg());
112 else txt = name();
113 return txt;
114}
115
116
117void Exception::extendedMessage(const std::string& arg)
118{
119 if (!arg.empty())
120 {
121 if (!_msg.empty()) _msg.append(": ");
122 _msg.append(arg);
123 }
124}
125
126
127void Exception::addBacktrace()
128{
129#ifdef POCO_EXCEPTION_BACKTRACE
130 if (NDC::hasBacktrace())
131 {
132 _msg.append(1, '\n').append(NDC::backtrace(2, 3));
133 }
134#endif
135}
136
137
138Exception* Exception::clone() const
139{
140 return new Exception(*this);
141}
142
143
144void Exception::rethrow() const
145{
146 throw *this;
147}
148
149
150POCO_IMPLEMENT_EXCEPTION(LogicException, Exception, "Logic exception")
151POCO_IMPLEMENT_EXCEPTION(AssertionViolationException, LogicException, "Assertion violation")
152POCO_IMPLEMENT_EXCEPTION(NullPointerException, LogicException, "Null pointer")
153POCO_IMPLEMENT_EXCEPTION(NullValueException, LogicException, "Null value")
154POCO_IMPLEMENT_EXCEPTION(BugcheckException, LogicException, "Bugcheck")
155POCO_IMPLEMENT_EXCEPTION(InvalidArgumentException, LogicException, "Invalid argument")
156POCO_IMPLEMENT_EXCEPTION(NotImplementedException, LogicException, "Not implemented")
157POCO_IMPLEMENT_EXCEPTION(RangeException, LogicException, "Out of range")
158POCO_IMPLEMENT_EXCEPTION(IllegalStateException, LogicException, "Illegal state")
159POCO_IMPLEMENT_EXCEPTION(InvalidAccessException, LogicException, "Invalid access")
160POCO_IMPLEMENT_EXCEPTION(SignalException, LogicException, "Signal received")
161POCO_IMPLEMENT_EXCEPTION(UnhandledException, LogicException, "Unhandled exception")
162
163POCO_IMPLEMENT_EXCEPTION(RuntimeException, Exception, "Runtime exception")
164POCO_IMPLEMENT_EXCEPTION(NotFoundException, RuntimeException, "Not found")
165POCO_IMPLEMENT_EXCEPTION(ExistsException, RuntimeException, "Exists")
166POCO_IMPLEMENT_EXCEPTION(TimeoutException, RuntimeException, "Timeout")
167POCO_IMPLEMENT_EXCEPTION(SystemException, RuntimeException, "System exception")
168POCO_IMPLEMENT_EXCEPTION(RegularExpressionException, RuntimeException, "Error in regular expression")
169POCO_IMPLEMENT_EXCEPTION(LibraryLoadException, RuntimeException, "Cannot load library")
170POCO_IMPLEMENT_EXCEPTION(LibraryAlreadyLoadedException, RuntimeException, "Library already loaded")
171POCO_IMPLEMENT_EXCEPTION(NoThreadAvailableException, RuntimeException, "No thread available")
172POCO_IMPLEMENT_EXCEPTION(PropertyNotSupportedException, RuntimeException, "Property not supported")
173POCO_IMPLEMENT_EXCEPTION(PoolOverflowException, RuntimeException, "Pool overflow")
174POCO_IMPLEMENT_EXCEPTION(NoPermissionException, RuntimeException, "No permission")
175POCO_IMPLEMENT_EXCEPTION(OutOfMemoryException, RuntimeException, "Out of memory")
176POCO_IMPLEMENT_EXCEPTION(DataException, RuntimeException, "Data error")
177
178POCO_IMPLEMENT_EXCEPTION(InterruptedException, RuntimeException, "Interrupted")
179POCO_IMPLEMENT_EXCEPTION(IndexOutOfBoundsException, RuntimeException, "Index out of bounds")
180POCO_IMPLEMENT_EXCEPTION(UnsupportedOperationException, RuntimeException, "Unsupported operation")
181POCO_IMPLEMENT_EXCEPTION(EmptyStackException, RuntimeException, "Empty stack")
182POCO_IMPLEMENT_EXCEPTION(StackOverflowException, RuntimeException, "Stack overflow")
183POCO_IMPLEMENT_EXCEPTION(ArithmeticException, RuntimeException, "Arithmetic error")
184
185POCO_IMPLEMENT_EXCEPTION(DataFormatException, DataException, "Bad data format")
186POCO_IMPLEMENT_EXCEPTION(SyntaxException, DataException, "Syntax error")
187POCO_IMPLEMENT_EXCEPTION(CircularReferenceException, DataException, "Circular reference")
188POCO_IMPLEMENT_EXCEPTION(PathSyntaxException, SyntaxException, "Bad path syntax")
189POCO_IMPLEMENT_EXCEPTION(IOException, RuntimeException, "I/O error")
190POCO_IMPLEMENT_EXCEPTION(ProtocolException, IOException, "Protocol error")
191POCO_IMPLEMENT_EXCEPTION(FileException, IOException, "File access error")
192POCO_IMPLEMENT_EXCEPTION(FileExistsException, FileException, "File exists")
193POCO_IMPLEMENT_EXCEPTION(FileNotFoundException, FileException, "File not found")
194POCO_IMPLEMENT_EXCEPTION(PathNotFoundException, FileException, "Path not found")
195POCO_IMPLEMENT_EXCEPTION(FileReadOnlyException, FileException, "File is read-only")
196POCO_IMPLEMENT_EXCEPTION(FileAccessDeniedException, FileException, "Access to file denied")
197POCO_IMPLEMENT_EXCEPTION(CreateFileException, FileException, "Cannot create file")
198POCO_IMPLEMENT_EXCEPTION(OpenFileException, FileException, "Cannot open file")
199POCO_IMPLEMENT_EXCEPTION(WriteFileException, FileException, "Cannot write file")
200POCO_IMPLEMENT_EXCEPTION(ReadFileException, FileException, "Cannot read file")
201POCO_IMPLEMENT_EXCEPTION(DirectoryNotEmptyException, FileException, "Directory not empty")
202POCO_IMPLEMENT_EXCEPTION(UnknownURISchemeException, RuntimeException, "Unknown URI scheme")
203POCO_IMPLEMENT_EXCEPTION(TooManyURIRedirectsException, RuntimeException, "Too many URI redirects")
204POCO_IMPLEMENT_EXCEPTION(URISyntaxException, SyntaxException, "Bad URI syntax")
205
206POCO_IMPLEMENT_EXCEPTION(ApplicationException, Exception, "Application exception")
207POCO_IMPLEMENT_EXCEPTION(BadCastException, RuntimeException, "Bad cast exception")
208
209
210} // namespace Poco
211