source: trunk/Include/Classes/System/Exception.ab@ 385

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

例外クラスの実装。ExceptionTestでSystem.Exceptionを使用するようにした。
StringBuilderでコメント化されていた例外を投げる処理を有効にした(除OutOfMemory)。
Str$の実装にSPrintfなどを使用するようにした。
毎回Object.ReferenceEquals(xxx, Nothing)と打つのが面倒なので、IsNothingを導入。

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