source: trunk/ab5.0/ablib/src/Classes/System/Exception.ab@ 605

Last change on this file since 605 was 602, checked in by イグトランス (egtra), 16 years ago

Exception.ToStringでHRESULTをFormatMessageで文字列化したものを出力するようにした(要#192)。

File size: 17.3 KB
Line 
1'Classses/System/Exception.ab
2
3#require <Classes/ActiveBasic/misc.ab>
4
5Namespace ActiveBasic
6'右のコメントは対応するCOR_E_ナントカのコード
7Const AB_E_EXCEPTION = &h80041500 '80131500
8Const AB_E_SYSTEM = &h80041501 '80131501
9Const AB_E_ARGUMENT = E_INVALIDARG '80070057
10Const AB_E_ARGUMENTOUTOFRANGE = E_INVALIDARG '80131502
11Const AB_E_INDEXOUTOFRANGE = &h80041508 '80131508
12Const AB_E_INVALIDOPERATION = &h80041509 '80131509
13Const AB_E_NOTSUPPORTED = &h80041515 '80131515
14Const AB_E_PLATFORMNOTSUPPORTED = &h80041539 '80131539
15Const AB_E_KEYNOTFOUND = &h80041577 '80131577
16
17End Namespace
18
19/*
20現時点で不要そうなもの(System名前空間直下のもののみ)
21AccessViolationException
22ArrayTypeMismatchException
23ArithmeticException
24BadImageFormatException
25DataMisalignedException
26FormatException
27InvalidCastException
28
29当分(一部は未来永劫)不要そうなもの(System名前空間直下のもののみ)
30AppDomainUnloadedException
31CannotUnloadAppDomainException
32ExecutionEngineException
33InvalidProgramException
34MemberAccessException
35 FieldAccessException
36 MethodAccessException
37 MissingMemberException
38MulticastNotSupportedException
39NullReferenceException
40OverflowException
41RankException
42StackOverflowException
43TypeInitializationException
44TypeLoadException
45TypeUnloadedException
46UnauthorizedAccessException
47*/
48
49Namespace System
50
51/*!
52@brief 例外クラスの基本クラス
53@author Egtra
54@date 2007/11/16
55*/
56Class Exception
57Public
58 /*!
59 @biref コンストラクタ
60 */
61 Sub Exception()
62 init(GetType().FullName, Nothing)
63 hr = ActiveBasic.AB_E_EXCEPTION
64 End Sub
65 /*!
66 @biref コンストラクタ
67 @param[in] message エラーメッセージ
68 */
69 Sub Exception(message As String)
70 init(message, Nothing)
71 hr = ActiveBasic.AB_E_EXCEPTION
72 End Sub
73 /*!
74 @biref コンストラクタ
75 @param[in] message エラーメッセージ
76 @param[in] innerException 内部例外
77 */
78 Sub Exception(message As String, innerException As Exception)
79 init(message, innerException)
80 hr = ActiveBasic.AB_E_EXCEPTION
81 End Sub
82
83 'Methods
84
85 /*!
86 @brief 基本例外を返す
87 @return 最初に投げられた大本の例外
88 */
89 Function GetBaseException() As Exception
90 GetBaseException = This
91 While ActiveBasic.IsNothing(GetBaseException.inner) = False
92 GetBaseException = GetBaseException.inner
93 Wend
94 End Function
95
96 /*!
97 @brief 文字列化する
98 @return エラー内容を表す文字列
99 */
100 Override Function ToString() As String
101 Imports ActiveBasic.Strings.Detail
102 If ActiveBasic.IsNothing(toStr) Then
103 Dim sb = New System.Text.StringBuilder
104 With sb
105 .Append(GetType().FullName).Append(": ")
106 .Append(Message)
107 .Append(" (Error code: ")
108 .Append(FormatIntegerX(hr As DWord, 8, 0, BPrefix))
109 Dim s = ActiveBasic.Windows.HResultToString(hr)
110 If Not String.IsNullOrEmpty(s) Then
111 .Append(" ").Append(s)
112 End If
113 .Append(")")
114 toStr = .ToString
115 End With
116 End If
117 Return toStr
118 End Function
119
120 'Properties
121
122 /*!
123 @brief 内部例外を返す
124 @return これが保持している内部例外。無ければNothing。
125 */
126 Function InnerException() As Exception
127 Return inner
128 End Function
129
130 /*!
131 @brief エラーメッセージの取得
132 */
133 Virtual Function Message() As String
134 Return msg
135 End Function
136
137 /*!
138 @brief この例外に関連付けられたヘルプへのURLもしくはURNの設定
139 */
140 Virtual Sub HelpLink(help As String)
141 helpLink = help
142 End Sub
143
144 /*!
145 @brief この例外に関連付けられたヘルプへのURLもしくはURNの取得
146 */
147 Virtual Function HelpLink() As String
148 Return helpLink
149 End Function
150
151 /*!
152 @brief この例外の発生元のアプリケーションもしくはオブジェクトの設定
153 */
154 Virtual Sub Source(source As String)
155 src = source
156 End Sub
157
158 /*!
159 @brief この例外の発生元のアプリケーションもしくはオブジェクトの取得
160 */
161 Virtual Function Source() As String
162 Return src
163 End Function
164
165 Const Function ErrorCode() As HRESULT
166 ErrorCode = hr
167 End Function
168Protected
169 /*!
170 @brief HRESULT値の設定
171 */
172 Sub HResult(hres As HRESULT)
173 hr = hres
174 End Sub
175
176 /*!
177 @brief HRESULT値の取得
178 */
179 Function HResult() As HRESULT
180 Return hr
181 End Function
182
183Private
184 Sub init(message As String, innerException As Exception)
185 msg = message
186 inner = innerException
187 End Sub
188
189 msg As String
190 toStr As String
191 inner As Exception
192 helpLink As String
193 src As String
194 hr As HRESULT
195End Class
196
197/*!
198@brief システム定義の例外の基底クラス
199@author Egtra
200@date 2007/11/17
201*/
202Class SystemException
203 Inherits Exception
204Public
205 /*!
206 @biref コンストラクタ
207 */
208 Sub SystemException()
209 Exception(GetType().FullName, Nothing)
210 HResult = ActiveBasic.AB_E_SYSTEM
211 End Sub
212 /*!
213 @biref コンストラクタ
214 @param[in] message エラーメッセージ
215 */
216 Sub SystemException(message As String)
217 Exception(message, Nothing)
218 HResult = ActiveBasic.AB_E_SYSTEM
219 End Sub
220 /*!
221 @biref コンストラクタ
222 @param[in] message エラーメッセージ
223 @param[in] innerException 内部例外
224 */
225 Sub SystemException(message As String, innerException As Exception)
226 Exception(message, innerException)
227 HResult = ActiveBasic.AB_E_SYSTEM
228 End Sub
229End Class
230
231/*!
232@brief 実引数に問題があることを表す例外
233@author Egtra
234@date 2007/11/19
235*/
236Class ArgumentException
237 Inherits SystemException
238Public
239 /*!
240 @biref コンストラクタ
241 */
242 Sub ArgumentException()
243 SystemException("One or more arguments have invalid value.", Nothing)
244 HResult = ActiveBasic.AB_E_ARGUMENT
245 End Sub
246 /*!
247 @biref コンストラクタ
248 @param[in] message エラーメッセージ
249 */
250 Sub ArgumentException(message As String)
251 SystemException(message, Nothing)
252 HResult = ActiveBasic.AB_E_ARGUMENT
253 End Sub
254 /*!
255 @biref コンストラクタ
256 @param[in] message エラーメッセージ
257 @param[in] innerException 内部例外
258 */
259 Sub ArgumentException(message As String, innerException As Exception)
260 SystemException(message, innerException)
261 HResult = ActiveBasic.AB_E_ARGUMENT
262 End Sub
263 /*!
264 @biref コンストラクタ
265 @param[in] message エラーメッセージ
266 @param[in] paramName 原因となった仮引数名
267 */
268 Sub ArgumentException(message As String, paramName As String)
269 SystemException(message, Nothing)
270 param = paramName
271 HResult = ActiveBasic.AB_E_ARGUMENT
272 End Sub
273 /*!
274 @biref コンストラクタ
275 @param[in] message エラーメッセージ
276 @param[in] innerException 内部例外
277 @param[in] paramName 原因となった仮引数名
278 */
279 Sub ArgumentException(message As String, paramName As String, innerException As Exception)
280 SystemException(message, innerException)
281 param = paramName
282 HResult = ActiveBasic.AB_E_ARGUMENT
283 End Sub
284
285 Override Function Message() As String
286 Dim sb = New System.Text.StringBuilder
287 sb.Append(param).Append(": ").Append(Super.Message)
288 Return sb.ToString
289 End Function
290
291 /*!
292 @brief この例外の発生原因の仮引数名の取得
293 */
294 Virtual Function Param() As String
295 Return param
296 End Function
297Private
298 param As String
299End Class
300
301/*!
302@brief NothingまたはNULLを受け付けない引数にそれらが渡されたことを表す例外
303@author Egtra
304@date 2007/11/19
305*/
306Class ArgumentNullException
307 Inherits ArgumentException
308Public
309 /*!
310 @biref コンストラクタ
311 */
312 Sub ArgumentNullException()
313 ArgumentException("One or more arguments have Nothing or Null value.", "", Nothing)
314 HResult = E_POINTER
315 End Sub
316 /*!
317 @biref コンストラクタ
318 @param[in] message エラーメッセージ
319 */
320 Sub ArgumentNullException(message As String)
321 ArgumentException(message, "", Nothing)
322 HResult = E_POINTER
323 End Sub
324 /*!
325 @biref コンストラクタ
326 @param[in] message エラーメッセージ
327 @param[in] innerException 内部例外
328 */
329 Sub ArgumentNullException(message As String, innerException As Exception)
330 ArgumentException(message, "", innerException)
331 HResult = E_POINTER
332 End Sub
333 /*!
334 @biref コンストラクタ
335 @param[in] message エラーメッセージ
336 @param[in] paramName 原因となった仮引数名
337 */
338 Sub ArgumentNullException(message As String, paramName As String)
339 ArgumentException(message, paramName)
340 HResult = E_POINTER
341 End Sub
342End Class
343
344/*!
345@brief 規定された範囲を超える実引数が渡されたことを表す例外
346@author Egtra
347@date 2007/11/19
348*/
349Class ArgumentOutOfRangeException
350 Inherits ArgumentException
351Public
352 /*!
353 @biref コンストラクタ
354 */
355 Sub ArgumentOutOfRangeException()
356 ArgumentException("One or more arguments ware out of range value.", "", Nothing)
357 HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE
358 End Sub
359 /*!
360 @biref コンストラクタ
361 @param[in] message エラーメッセージ
362 */
363 Sub ArgumentOutOfRangeException(message As String)
364 ArgumentException(message, "", Nothing)
365 HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE
366 End Sub
367 /*!
368 @biref コンストラクタ
369 @param[in] message エラーメッセージ
370 @param[in] innerException 内部例外
371 */
372 Sub ArgumentOutOfRangeException(message As String, innerException As Exception)
373 ArgumentException(message, "", innerException)
374 HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE
375 End Sub
376 /*!
377 @biref コンストラクタ
378 @param[in] message エラーメッセージ
379 @param[in] paramName 原因となった仮引数名
380 */
381 Sub ArgumentOutOfRangeException(message As String, paramName As String)
382 ArgumentException(message, paramName, Nothing)
383 HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE
384 End Sub
385 /*!
386 @biref コンストラクタ
387 @param[in] message エラーメッセージ
388 @param[in] actualValue 問題となった仮引数の値
389 @param[in] paramName 原因となった仮引数名
390 */
391 Sub ArgumentOutOfRangeException(message As String, actualValue As Object, paramName As String)
392 ArgumentException(message, paramName, Nothing)
393 actualValueObject = actualValue
394 HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE
395 End Sub
396
397 /*!
398 @brief この例外の発生原因の実引数の値の取得
399 */
400 Virtual Function ActualValue() As Object
401 Return actualValueObject
402 End Function
403
404 Override Function Message() As String
405 If ActiveBasic.IsNothing(actualValueObject) Then
406 Return Super.Message
407 Else
408 Dim sb = New System.Text.StringBuilder
409 sb.Append(Param).Append(" = ").Append(actualValueObject)
410 sb.Append(": ").Append(Super.Message)
411 Return sb.ToString
412 End If
413 End Function
414Private
415 actualValueObject As Object
416End Class
417
418/*!
419@brief 配列の範囲外の要素を読み書きしようとしたことを表す例外
420@author Egtra
421@date 2007/11/19
422*/
423Class IndexOutOfRangeException
424 Inherits SystemException
425Public
426 /*!
427 @biref コンストラクタ
428 */
429 Sub IndexOutOfRangeException()
430 SystemException("The index was out of range value.", Nothing)
431 HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE
432 End Sub
433 /*!
434 @biref コンストラクタ
435 @param[in] message エラーメッセージ
436 */
437 Sub IndexOutOfRangeException(message As String)
438 SystemException(message, Nothing)
439 HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE
440 End Sub
441 /*!
442 @biref コンストラクタ
443 @param[in] message エラーメッセージ
444 @param[in] innerException 内部例外
445 */
446 Sub IndexOutOfRangeException(message As String, innerException As Exception)
447 SystemException(message, innerException)
448 HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE
449 End Sub
450End Class
451
452/*!
453@brief 無効な操作を行おうとしたことを表す例外
454@author Egtra
455@date 2007/11/19
456*/
457Class InvalidOperationException
458 Inherits SystemException
459Public
460 /*!
461 @biref コンストラクタ
462 */
463 Sub InvalidOperationException()
464 SystemException("The operation is invalid.", Nothing)
465 HResult = ActiveBasic.AB_E_INVALIDOPERATION
466 End Sub
467 /*!
468 @biref コンストラクタ
469 @param[in] message エラーメッセージ
470 */
471 Sub InvalidOperationException(message As String)
472 SystemException(message, Nothing)
473 HResult = ActiveBasic.AB_E_INVALIDOPERATION
474 End Sub
475 /*!
476 @biref コンストラクタ
477 @param[in] message エラーメッセージ
478 @param[in] innerException 内部例外
479 */
480 Sub InvalidOperationException(message As String, innerException As Exception)
481 SystemException(message, innerException)
482 HResult = ActiveBasic.AB_E_INVALIDOPERATION
483 End Sub
484End Class
485
486/*!
487@brief 博されたオブジェクトを操作しようとしたことを表す例外
488@author Egtra
489@date 2007/12/06
490*/
491Class ObjectDisposedException
492 Inherits InvalidOperationException
493Public
494 /*!
495 @biref コンストラクタ
496 */
497 Sub ObjectDisposedException()
498 InvalidOperationException("The object has been disposed.", Nothing)
499 HResult = ActiveBasic.AB_E_INVALIDOPERATION
500 End Sub
501 /*!
502 @biref コンストラクタ
503 @param[in] message エラーメッセージ
504 */
505 Sub ObjectDisposedException(message As String)
506 InvalidOperationException(message, Nothing)
507 HResult = ActiveBasic.AB_E_INVALIDOPERATION
508 End Sub
509 /*!
510 @biref コンストラクタ
511 @param[in] message エラーメッセージ
512 @param[in] innerException 内部例外
513 */
514 Sub ObjectDisposedException(message As String, innerException As Exception)
515 InvalidOperationException(message, innerException)
516 HResult = ActiveBasic.AB_E_INVALIDOPERATION
517 End Sub
518 /*!
519 @biref コンストラクタ
520 @param[in] objectName 操作しようとしたオブジェクトの名前
521 @param[in] message エラーメッセージ
522 */
523 Sub ObjectDisposedException(objectName As String, message As String)
524 InvalidOperationException(message, Nothing)
525 HResult = ActiveBasic.AB_E_INVALIDOPERATION
526 objName = " object: " + objectName
527 End Sub
528 /*!
529 @brief この例外が発生したとき、操作しようとしていたオブジェクトの名前を返す。
530 */
531 Function ObjectName() As String
532 Return objName
533 End Function
534
535 Override Function ToString() As String
536 ToString = Super.ToString()
537 If Not ActiveBasic.IsNothing(objName) Then
538 ToString += objName
539 End If
540 End Function
541Private
542 objName As String
543End Class
544
545/*!
546@brief そのメソッド・関数ないし操作が実装されていないことを表す例外
547@author Egtra
548@date 2007/11/19
549*/
550Class NotImplementedException
551 Inherits SystemException
552Public
553 /*!
554 @biref コンストラクタ
555 */
556 Sub NotImplementedException()
557 SystemException("Not implemented.", Nothing)
558 HResult = E_NOTIMPL
559 End Sub
560 /*!
561 @biref コンストラクタ
562 @param[in] message エラーメッセージ
563 */
564 Sub NotImplementedException(message As String)
565 SystemException(message, Nothing)
566 HResult = E_NOTIMPL
567 End Sub
568 /*!
569 @biref コンストラクタ
570 @param[in] message エラーメッセージ
571 @param[in] innerException 内部例外
572 */
573 Sub NotImplementedException(message As String, innerException As Exception)
574 SystemException(message, innerException)
575 HResult = E_NOTIMPL
576 End Sub
577End Class
578
579/*!
580@brief 対応していないメソッド・関数ないし操作を行おうとしたことを表す例外
581@author Egtra
582@date 2007/11/19
583*/
584Class NotSupportedException
585 Inherits SystemException
586Public
587 /*!
588 @biref コンストラクタ
589 */
590 Sub NotSupportedException()
591 SystemException("This operation is not supported,", Nothing)
592 HResult = ActiveBasic.AB_E_NOTSUPPORTED
593 End Sub
594 /*!
595 @biref コンストラクタ
596 @param[in] message エラーメッセージ
597 */
598 Sub NotSupportedException(message As String)
599 SystemException(message, Nothing)
600 HResult = ActiveBasic.AB_E_NOTSUPPORTED
601 End Sub
602 /*!
603 @biref コンストラクタ
604 @param[in] message エラーメッセージ
605 @param[in] innerException 内部例外
606 */
607 Sub NotSupportedException(message As String, innerException As Exception)
608 SystemException(message, innerException)
609 HResult = ActiveBasic.AB_E_NOTSUPPORTED
610 End Sub
611End Class
612
613/*!
614@brief 実行しているプラットフォームで対応していないメソッド・関数ないし操作を行おうとしたことを表す例外
615@author Egtra
616@date 2007/11/19
617*/
618Class PlatformNotSupportedException
619 Inherits NotSupportedException
620Public
621 /*!
622 @biref コンストラクタ
623 */
624 Sub PlatformNotSupportedException()
625 NotSupportedException("This operation is not supported in this platform.", Nothing)
626 HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED
627 End Sub
628 /*!
629 @biref コンストラクタ
630 @param[in] message エラーメッセージ
631 */
632 Sub PlatformNotSupportedException(message As String)
633 NotSupportedException(message, Nothing)
634 HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED
635 End Sub
636 /*!
637 @biref コンストラクタ
638 @param[in] message エラーメッセージ
639 @param[in] innerException 内部例外
640 */
641 Sub PlatformNotSupportedException(message As String, innerException As Exception)
642 NotSupportedException(message, innerException)
643 HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED
644 End Sub
645End Class
646
647/*!
648@brief 操作が取り止められたことを表す例外
649@author Egtra
650@date 2007/11/19
651@todo HResultの調査
652*/
653Class OperationCanceledException
654 Inherits SystemException
655Public
656 /*!
657 @biref コンストラクタ
658 */
659 Sub OperationCanceledException()
660 SystemException("The operation was canceled.", Nothing)
661 End Sub
662 /*!
663 @biref コンストラクタ
664 @param[in] message エラーメッセージ
665 */
666 Sub OperationCanceledException(message As String)
667 SystemException(message, Nothing)
668 End Sub
669 /*!
670 @biref コンストラクタ
671 @param[in] message エラーメッセージ
672 @param[in] innerException 内部例外
673 */
674 Sub OperationCanceledException(message As String, innerException As Exception)
675 SystemException(message, innerException)
676 End Sub
677End Class
678
679/*!
680@brief メモリ不足例外
681@author Egtra
682@date 2007/11/19
683@todo HResultの調査
684*/
685Class OutOfMemoryException
686 Inherits SystemException
687Public
688 /*!
689 @biref コンストラクタ
690 */
691 Sub OutOfMemoryException()
692 SystemException("Out of memory.", Nothing)
693 HResult = E_OUTOFMEMORY
694 End Sub
695 /*!
696 @biref コンストラクタ
697 @param[in] message エラーメッセージ
698 */
699 Sub OutOfMemoryException(message As String)
700 SystemException(message, Nothing)
701 HResult = E_OUTOFMEMORY
702 End Sub
703 /*!
704 @biref コンストラクタ
705 @param[in] message エラーメッセージ
706 @param[in] innerException 内部例外
707 */
708 Sub OutOfMemoryException(message As String, innerException As Exception)
709 SystemException(message, innerException)
710 HResult = E_OUTOFMEMORY
711 End Sub
712End Class
713
714
715End Namespace
Note: See TracBrowser for help on using the repository browser.