DCL 4.0
Loading...
Searching...
No Matches
Exception.cpp
Go to the documentation of this file.
1
2#include <dcl/Config.h>
3
4#if __DCL_WINDOWS
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/Exception.h>
18#include <dcl/Numeric.h>
19
20#if __DCL_DEBUG
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 __pCause = _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 {
51 sb += p->className();
52#if __DCL_HAVE_ALLOC_DEBUG
53 char_t filename[100];
54 unsigned int line;
55 if (DCLDebugAllocGetPosition(p, filename,
56 __countof(filename, char_t), &line)) {
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->__pCause;
67 if (p)
68 sb += L"\n";
69 }
70
71 return sb;
72}
73
75{
76 if (__pCause)
77 {
78 __pCause->destroy();
79// __pCause = NULL;
80 }
82}
83
85
86OutOfMemoryException::OutOfMemoryException()
87{
88}
89
91
93 : Exception(_cause)
94{
95 __errorNo = 0;
96}
97
98SysError::SysError(uint32_t _errorNo)
99{
100 __errorNo = _errorNo;
101}
102
103String SysError::toString() const
104{
105 StringBuilder sb;
106 if (__errorNo & DCL_ERROR_MASK)
107 {
108 ;
109 }
110#if __DCL_WINDOWS
111 else if (__errorNo & WINAPI_ERROR_MASK)
112 {
113 uint32_t _errorNo = __errorNo & ERROR_VALUE_MASK;
114 wchar_t* msg = NULL;
115 DWORD dwLength = ::FormatMessageW(
116 FORMAT_MESSAGE_ALLOCATE_BUFFER |
117 FORMAT_MESSAGE_FROM_SYSTEM |
118 FORMAT_MESSAGE_IGNORE_INSERTS,
119 NULL,
120 _errorNo,
121 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
122 (LPWSTR) &msg,
123 0,
124 NULL
125 );
126
127 if (dwLength > 0)
128 {
129 for ( ; dwLength; dwLength--)
130 if (!(msg[dwLength - 1] == L'\r' || msg[dwLength - 1] == L'\n'))
131 break;
132
133 sb.assign(msg, dwLength);
134 LocalFree(msg);
135
136 sb += L" (ERROR=";
137 sb += UInt32::toString(_errorNo, 10);
138 sb += L")";
139 }
140 else
141 sb = L"::FormatMessage Fail!";
142
143 }
144#endif
145 else
146 {
147 // errno
148 uint32_t _errorNo = __errorNo & ERROR_VALUE_MASK;
149
150#if __DCL_WINDOWS
151 CharBuffer* buf = CharBuffer::create(256);
152 if (_wcserror_s(buf->data(), buf->__allocLength, __errorNo))
153 buf->release();
154 else
155 {
156 buf->__dataLength = String::length(buf->data());
157 __DCL_ASSERT(buf->data()[buf->__dataLength] == __T('\0'));
158 sb.assign(buf);
159 buf->release();
160 }
161#else
162 // UNIX
163 String s;
164 if (__strerror(s, _errorNo) == 0)
165 sb += s;
166#endif
167
168 sb += L" (errno=" + UInt32::toString(_errorNo, 10) + L")";
169 }
170 return sb;
171}
172
174
175IOException::IOException(const String& _name, Exception* _cause)
176 : SysError(_cause)
177{
178 __message = L"\"" + _name + L"\"";
179}
180
181IOException::IOException(const String& _name, uint32_t _errorNo)
182 : SysError(WINAPI_ERROR(_errorNo))
183{
184 __message = L"\"" + _name + L"\"";
185}
186
187IOException::IOException(const String& _name, const String& _message)
188{
189 __message = L"\"" + _name + L"\"" + __T(" ") + _message;
190}
191
193{
194 StringBuilder r = __message;
195 if (SysError::errorNo() != 0)
196 r += __T(" ") + SysError::toString();
197
198 return r;
199}
200
202
204 : Exception(NULL)
205{
206 __message = L"invalid index key: " + _key;
207}
208
209InvalidIndexException::InvalidIndexException(ssize_t _minValid, ssize_t _maxValid, size_t _errorValue)
210 : Exception(NULL)
211{
212 __message = String::format(
213 L"invalid index. valud: %d, valid range: %d <= valud <= %d",
214 _errorValue,
215 _minValid,
216 _maxValid
217 );
218}
219
221{
222 return __message;
223}
224
226
227ParseException::ParseException(const String& _message)
228 : Exception(NULL)
229{
230 __message = _message;
231 __sourceOffset = -1;
232}
233
234ParseException::ParseException(const String& _message, size_t _sourceOffset)
235 : Exception(NULL)
236{
237 __message = _message;
238 __sourceOffset = _sourceOffset;
239}
240
242{
243 StringBuilder sb = __message;
244 if (__sourceOffset >= 0)
245 {
246 sb += L": offset:";
247#if __WORDSIZE == 64
248 sb += UInt64::toString(__sourceOffset, 10);
249#else
250 sb += UInt32::toString(__sourceOffset, 10);
251#endif
252 }
253 return sb;
254}
255
257
258// eInvalidNumberString
259NumericConvertException::NumericConvertException(
260 const String& _number,
261 int _radix,
262 size_t _sourceOffset
263 )
264 : ParseException(_number, _sourceOffset)
265{
266 __error = InvalidNumberString;
267 __radix = _radix;
268}
269
270// eOverflow or eUnderflow
271NumericConvertException::NumericConvertException(
272 Error _error,
273 const String& _number,
274 int _radix // = 0,
275 )
276 : ParseException(_number)
277{
278 __DCL_ASSERT(_error == Overflow || _error == Underflow);
279 __error = _error;
280 __radix = _radix;
281}
282
284{
285 StringBuilder sb;
286 if (__error == InvalidNumberString)
287 {
288 sb = L"Invalid number string: \""
289 + ParseException::__message
290 + L"\"";
291
292 if (__radix > 0)
293 {
294 sb += L", radix:";
295 sb += Int32::toString(__radix, 10);
296 }
297
298 if (ParseException::__sourceOffset >= 0)
299 {
300 sb += L", offset:";
301#if __WORDSIZE == 64
302 sb += Int64::toString(ParseException::__sourceOffset, 10);
303#else
304 sb += Int32::toString(ParseException::__sourceOffset, 10);
305#endif
306 }
307 }
308 else
309 {
310 if (__error == Overflow)
311 sb = L"Numerical result overflow \"";
312 else
313 sb = L"Numerical result underflow \"";
314 sb += ParseException::__message + L"\"";
315
316 if (__radix > 0)
317 {
318 sb += L", radix:";
319 sb += Int32::toString(__radix, 10);
320 }
321 }
322
323 return sb;
324}
325
332
333GenerialException::GenerialException(const wchar_t* pszErrorMessage)
334 : Exception(NULL)
335{
336 __DCL_ASSERT(pszErrorMessage != NULL);
337 __message = pszErrorMessage;
338}
339
341{
342 StringBuilder sb = __message;
343
345 sb += L": See! Detail";
346
347 return sb;
348}
349
351
353 const char_t* _filename,
354 unsigned int _line,
355 const char_t* _expr,
356 const char_t* _message
357 )
358 : Exception(NULL)
359{
360 __DCL_ASSERT(_filename != NULL);
361 __DCL_ASSERT(_expr != NULL);
362
363 __filename = _filename;
364 __line = _line;
365
366 __expr = _expr;
367 if (_message)
368 __message = _message;
369}
370
372{
373 return format(__filename, __line, __expr,
374 __message.isEmpty() ? NULL : __message.data());
375}
376
378 const char_t* _filename,
379 unsigned int _line,
380 const char_t* _expr,
381 const char_t* _message
382 )
383{
384 StringBuilder sb = L"Assertion Failed ! '";
385 sb += _expr;
386 sb += L"'";
387 if (_message)
388 {
389 sb += L" ";
390 sb += _message;
391 }
392 sb += L", File ";
393 sb += _filename;
394 sb += L":";
395 sb += Int32::toString((int32_t)_line, 10);
396
397
398 return sb;
399}
400
401
402/*
403
404IMPLEMENT_CLASSINFO(SocketException, SysCallException)
405
406SocketException::SocketException(const char* pszErrorMessage)
407 : Exception(NULL)
408{
409 __DCL_ASSERT(pszErrorMessage != NULL);
410 __message = pszErrorMessage;
411}
412
413String SocketException::toString() const
414{
415 return Exception::toStringHeader() + __message;
416}
417
418*/
419
420__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:340
#define __countof(array, type)
Definition Config.h:365
wchar_t char_t
Definition Config.h:275
#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
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
ByteString r
ByteBuffer * buf
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 * __pCause
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:87
String toString(unsigned _base=10) const
Definition Numeric.inl:111
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:99
String toString(unsigned _base=10) const
Definition Numeric.inl:123
String toString(unsigned _base=10) const
Definition Numeric.inl:54