DCL 3.7.4
Loading...
Searching...
No Matches
Exception.cpp
Go to the documentation of this file.
1
2#include <dcl/Config.h>
3
4#ifdef __WINNT__
5 #include <windows.h>
6#else
7 #include <dcl/_string.h>
8#endif
9
10#include <dcl/Object.h>
11
12#if __DCL_HAVE_ALLOC_DEBUG
13#undef __DCL_ALLOC_LEVEL
14#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
15#endif
16
17#include <dcl/Numeric.h>
18#include <dcl/Exception.h>
19
20#if __DCL_HAVE_THIS_FILE__
21#undef __THIS_FILE__
22static const char_t* __THIS_FILE__ = __T("dcl/Exception.cpp");
23#endif
24
25__DCL_BEGIN_NAMESPACE
26
28
30{
31 __cause = _cause;
32
33#if __DCL_HAVE_ALLOC_DEBUG
34 // DCL 내부적으로 new 연산자에 대하여 메모리 검사 플래그가 false
35 // 로 되어 있다. Exception 객체는 반드시 destroy 되어야 한다.
36 DCLDebugAllocSetCheckFlag(this, true);
37#endif
38}
39
40String Exception::toString() const
41{
42 return String();
43}
44
46{
47 StringBuilder sb;
48 const Exception* p = this;
49 while(p) {
50 sb += p->className();
51#if __DCL_HAVE_ALLOC_DEBUG
52 char_t filename[100];
53 unsigned int line;
54 if (DCLDebugAllocGetPosition(p, filename,
55 __countof(filename, char_t), &line
56 )) {
57 sb += L": ";
58 sb.append(filename);
59 sb += L":";
60 sb += UInteger::toString(line, 10);
61 }
62#endif
63 sb += L": ";
64 sb += p->toString();
65
66 p = p->__cause;
67 if (p)
68 sb += L"\n";
69 }
70
71 return sb;
72}
73
75{
76 if (__cause) {
77 __cause->destroy();
78// __cause = NULL;
79 }
81}
82
84
85OutOfMemoryException::OutOfMemoryException()
86{
87}
88
90
92 : Exception(_cause)
93{
94 __errorNo = 0;
95}
96
97SysError::SysError(uint32_t _errorNo)
98{
99 __errorNo = _errorNo;
100}
101
102String SysError::toString() const
103{
104 StringBuilder sb;
105 if (__errorNo & DCL_ERROR_MASK) {
106 ;
107 }
108#ifdef __WINNT__
109 else if (__errorNo & WINAPI_ERROR_MASK) {
110 uint32_t _errorNo = __errorNo & ERROR_VALUE_MASK;
111 wchar_t* msg = NULL;
112 DWORD dwLength = ::FormatMessageW(
113 FORMAT_MESSAGE_ALLOCATE_BUFFER |
114 FORMAT_MESSAGE_FROM_SYSTEM |
115 FORMAT_MESSAGE_IGNORE_INSERTS,
116 NULL,
117 _errorNo,
118 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
119 (LPWSTR) &msg,
120 0,
121 NULL
122 );
123
124 if (dwLength > 0) {
125 for ( ; dwLength; dwLength--)
126 if (!(msg[dwLength - 1] == L'\r' || msg[dwLength - 1] == L'\n'))
127 break;
128
129 sb.assign(msg, dwLength);
130 LocalFree(msg);
131
132 sb += L" (ERROR=";
133 sb += UInt32::toString(_errorNo, 10);
134 sb += L")";
135 }
136 else
137 sb = L"::FormatMessage Fail!";
138 }
139#endif
140 else {
141 // errno
142 uint32_t _errorNo = __errorNo & ERROR_VALUE_MASK;
143
144#ifdef __WINNT__
145 CharBuffer* buf = CharBuffer::create(256);
146 if (_wcserror_s(buf->data(), buf->__allocLength, __errorNo))
147 buf->release();
148 else {
149 buf->__dataLength = String::length(buf->data());
150 __DCL_ASSERT(buf->data()[buf->__dataLength] == __T('\0'));
151 sb.assign(buf);
152 buf->release();
153 }
154#else
155 // UNIX
156 String s;
157 if (__strerror(s, _errorNo) == 0)
158 sb += s;
159#endif
160 sb += L" (errno=" + UInt32::toString(_errorNo, 10) + L")";
161 }
162 return sb;
163}
164
166
167IOException::IOException(const String& _name, Exception* _cause)
168 : SysError(_cause)
169{
170 __message = L"\"" + _name + L"\"";
171}
172
173IOException::IOException(const String& _name, uint32_t _errorNo)
174 : SysError(WINAPI_ERROR(_errorNo))
175{
176 __message = L"\"" + _name + L"\"";
177}
178
179IOException::IOException(const String& _name, const String& _message)
180{
181 __message = L"\"" + _name + L"\"" + __T(" ") + _message;
182}
183
185{
186 StringBuilder r = __message;
187 if (SysError::errorNo() != 0)
188 r += __T(" ") + SysError::toString();
189
190 return r;
191}
192
194
196 : Exception(NULL)
197{
198 __message = L"invalid index key: " + _key;
199}
200
201InvalidIndexException::InvalidIndexException(ssize_t _minValid, ssize_t _maxValid, size_t _errorValue)
202 : Exception(NULL)
203{
204 __message = String::format(
205 L"invalid index. valud: %d, valid range: %d <= valud <= %d",
206 _errorValue,
207 _minValid,
208 _maxValid
209 );
210}
211
213{
214 return __message;
215}
216
218
219ParseException::ParseException(const String& _message)
220 : Exception(NULL)
221{
222 __message = _message;
223 __sourceOffset = -1;
224}
225
226ParseException::ParseException(const String& _message, size_t _sourceOffset)
227 : Exception(NULL)
228{
229 __message = _message;
230 __sourceOffset = _sourceOffset;
231}
232
234{
235 StringBuilder sb = __message;
236 if (__sourceOffset >= 0) {
237 sb += L": offset:";
238#if __WORDSIZE == 64
239 sb += UInt64::toString(__sourceOffset, 10);
240#else
241 sb += UInt32::toString(__sourceOffset, 10);
242#endif
243 }
244 return sb;
245}
246
248
249// eInvalidNumberString
250NumericConvertException::NumericConvertException(
251 const String& _number,
252 int _radix,
253 size_t _sourceOffset
254) : ParseException(_number, _sourceOffset)
255{
256 __error = InvalidNumberString;
257 __radix = _radix;
258}
259
260// eOverflow or eUnderflow
261NumericConvertException::NumericConvertException(
262 Error _error,
263 const String& _number,
264 int _radix // = 0,
265) : ParseException(_number)
266{
267 __DCL_ASSERT(_error == Overflow || _error == Underflow);
268 __error = _error;
269 __radix = _radix;
270}
271
273{
274 StringBuilder sb;
275 if (__error == InvalidNumberString) {
276 sb = L"Invalid number string: \""
277 + ParseException::__message
278 + L"\"";
279
280 if (__radix > 0) {
281 sb += L", radix:";
282 sb += Int32::toString(__radix, 10);
283 }
284
285 if (ParseException::__sourceOffset >= 0) {
286 sb += L", offset:";
287#if __WORDSIZE == 64
288 sb += Int64::toString(ParseException::__sourceOffset, 10);
289#else
290 sb += Int32::toString(ParseException::__sourceOffset, 10);
291#endif
292 }
293 }
294 else {
295 if (__error == Overflow)
296 sb = L"Numerical result overflow \"";
297 else
298 sb = L"Numerical result underflow \"";
299 sb += ParseException::__message + L"\"";
300
301 if (__radix > 0) {
302 sb += L", radix:";
303 sb += Int32::toString(__radix, 10);
304 }
305 }
306
307 return sb;
308}
309
316
317GenerialException::GenerialException(const wchar_t* pszErrorMessage)
318 : Exception(NULL)
319{
320 __DCL_ASSERT(pszErrorMessage != NULL);
321 __message = pszErrorMessage;
322}
323
325{
326 StringBuilder sb = __message;
327
329 sb += L": See! Detail";
330
331 return sb;
332}
333
335
337 const char_t* _filename,
338 unsigned int _line,
339 const char_t* _expr,
340 const char_t* _message
341) : Exception(NULL)
342{
343 __DCL_ASSERT(_filename != NULL);
344 __DCL_ASSERT(_expr != NULL);
345
346 __filename = _filename;
347 __line = _line;
348
349 __expr = _expr;
350 if (_message)
351 __message = _message;
352}
353
355{
356 return format(__filename, __line, __expr,
357 __message.isEmpty() ? NULL : __message.data()
358 );
359}
360
362 const char_t* _filename,
363 unsigned int _line,
364 const char_t* _expr,
365 const char_t* _message
366)
367{
368 StringBuilder sb = L"Assertion Failed ! '";
369 sb += _expr;
370 sb += L"'";
371 if (_message) {
372 sb += L" ";
373 sb += _message;
374 }
375 sb += L", File ";
376 sb += _filename;
377 sb += L":";
378 sb += Int32::toString((int32_t)_line, 10);
379
380 return sb;
381}
382
383/*
384IMPLEMENT_CLASSINFO(SocketException, SysCallException)
385
386SocketException::SocketException(const char* pszErrorMessage)
387 : Exception(NULL)
388{
389 __DCL_ASSERT(pszErrorMessage != NULL);
390 __message = pszErrorMessage;
391}
392
393String SocketException::toString() const
394{
395 return Exception::toStringHeader() + __message;
396}
397*/
398
399__DCL_END_NAMESPACE
DCLCAPI int __strerror(String &_r, int _errnum)
Definition _string.cpp:21
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
#define __countof(array, type)
Definition Config.h:336
wchar_t char_t
Definition Config.h:247
#define DCL_ERROR_MASK
Definition Exception.h:71
#define WINAPI_ERROR_MASK
Definition Exception.h:70
#define WINAPI_ERROR(n)
Definition Exception.h:72
#define ERROR_VALUE_MASK
Definition Exception.h:68
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:245
#define __T(str)
Definition Object.h:60
static String format(const char_t *_filename, unsigned int _line, const char_t *_expr, const char_t *_message)
AssertError(const char_t *_filename, unsigned int _line, const char_t *_expr, const char_t *_message)
virtual String toString() const
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74
Exception(Exception *_cause=NULL)
String toStringAll() const
Definition Exception.cpp:45
Exception * __cause
Definition Exception.h:29
GenerialException(Exception *_cause)
virtual String toString() const
String __message
Definition Exception.h:94
virtual String toString() const
IOException(const String &_name, Exception *_cause)
String toString(unsigned _base=10) const
Definition Numeric.inl:93
String toString(unsigned _base=10) const
Definition Numeric.inl:117
InvalidIndexException(const String &_key)
virtual String toString() const
virtual void destroy()
Definition Object.cpp:192
String className() const
Definition Object.cpp:163
ParseException(const String &_message)
virtual String toString() const
uint32_t errorNo() const
Definition Exception.h:78
virtual String toString() const
SysError(Exception *_cause=NULL)
String toString(unsigned _base=10) const
Definition Numeric.inl:105
String toString(unsigned _base=10) const
Definition Numeric.inl:129
String toString(unsigned _base=10) const
Definition Numeric.inl:57