'Classses/System/Exception.ab #require Namespace ActiveBasic '右のコメントは対応するCOR_E_ナントカのコード Const AB_E_EXCEPTION = &h80041500 '80131500 Const AB_E_SYSTEM = &h80041501 '80131501 Const AB_E_ARGUMENT = E_INVALIDARG '80070057 Const AB_E_ARGUMENTOUTOFRANGE = E_INVALIDARG '80131502 Const AB_E_INDEXOUTOFRANGE = &h80041508 '80131508 Const AB_E_INVALIDOPERATION = &h80041509 '80131509 Const AB_E_NOTSUPPORTED = &h80041515 '80131515 Const AB_E_PLATFORMNOTSUPPORTED = &h80041539 '80131539 Const AB_E_KEYNOTFOUND = &h80041577 '80131577 Const AB_E_ARITHMETIC = &h80040216 '80070216 Const AB_E_OVERFLOW = &h80041516 '80131516 End Namespace /* 現時点で不要そうなもの(System名前空間直下のもののみ) AccessViolationException ArrayTypeMismatchException ArithmeticException BadImageFormatException DataMisalignedException FormatException InvalidCastException 当分(一部は未来永劫)不要そうなもの(System名前空間直下のもののみ) AppDomainUnloadedException CannotUnloadAppDomainException ExecutionEngineException InvalidProgramException MemberAccessException FieldAccessException MethodAccessException MissingMemberException MulticastNotSupportedException NullReferenceException RankException StackOverflowException TypeInitializationException TypeLoadException TypeUnloadedException UnauthorizedAccessException */ Namespace System /*! @brief 例外クラスの基本クラス @author Egtra @date 2007/11/16 */ Class Exception Public /*! @biref コンストラクタ */ Sub Exception() init(GetType().FullName, Nothing) hr = ActiveBasic.AB_E_EXCEPTION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub Exception(message As String) init(message, Nothing) hr = ActiveBasic.AB_E_EXCEPTION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub Exception(message As String, innerException As Exception) init(message, innerException) hr = ActiveBasic.AB_E_EXCEPTION End Sub 'Methods /*! @brief 基本例外を返す @return 最初に投げられた大本の例外 */ Function GetBaseException() As Exception GetBaseException = This While ActiveBasic.IsNothing(GetBaseException.inner) = False GetBaseException = GetBaseException.inner Wend End Function /*! @brief 文字列化する @return エラー内容を表す文字列 */ Override Function ToString() As String Imports ActiveBasic.Strings.Detail If ActiveBasic.IsNothing(toStr) Then Dim sb = New System.Text.StringBuilder With sb .Append(GetType().FullName).Append(": ") .Append(Message) .Append(" (Error code: ") .Append(FormatIntegerX(hr As DWord, 8, 0, BPrefix)) Dim s = ActiveBasic.Windows.HResultToString(hr) If Not String.IsNullOrEmpty(s) Then .Append(" ").Append(s) End If .Append(")") toStr = .ToString End With End If Return toStr End Function 'Properties /*! @brief 内部例外を返す @return これが保持している内部例外。無ければNothing。 */ Function InnerException() As Exception Return inner End Function /*! @brief エラーメッセージの取得 */ Virtual Function Message() As String Return msg End Function /*! @brief この例外に関連付けられたヘルプへのURLもしくはURNの設定 */ Virtual Sub HelpLink(help As String) helpLink = help End Sub /*! @brief この例外に関連付けられたヘルプへのURLもしくはURNの取得 */ Virtual Function HelpLink() As String Return helpLink End Function /*! @brief この例外の発生元のアプリケーションもしくはオブジェクトの設定 */ Virtual Sub Source(source As String) src = source End Sub /*! @brief この例外の発生元のアプリケーションもしくはオブジェクトの取得 */ Virtual Function Source() As String Return src End Function Const Function ErrorCode() As HRESULT ErrorCode = hr End Function Protected /*! @brief HRESULT値の設定 */ Sub HResult(hres As HRESULT) hr = hres End Sub /*! @brief HRESULT値の取得 */ Function HResult() As HRESULT Return hr End Function Private Sub init(message As String, innerException As Exception) msg = message inner = innerException End Sub msg As String toStr As String inner As Exception helpLink As String src As String hr As HRESULT End Class /*! @brief システム定義の例外の基底クラス @author Egtra @date 2007/11/17 */ Class SystemException Inherits Exception Public /*! @biref コンストラクタ */ Sub SystemException() Exception(GetType().FullName, Nothing) HResult = ActiveBasic.AB_E_SYSTEM End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub SystemException(message As String) Exception(message, Nothing) HResult = ActiveBasic.AB_E_SYSTEM End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub SystemException(message As String, innerException As Exception) Exception(message, innerException) HResult = ActiveBasic.AB_E_SYSTEM End Sub End Class /*! @brief 実引数に問題があることを表す例外 @author Egtra @date 2007/11/19 */ Class ArgumentException Inherits SystemException Public /*! @biref コンストラクタ */ Sub ArgumentException() SystemException("One or more arguments have invalid value.", Nothing) HResult = ActiveBasic.AB_E_ARGUMENT End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub ArgumentException(message As String) SystemException(message, Nothing) HResult = ActiveBasic.AB_E_ARGUMENT End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub ArgumentException(message As String, innerException As Exception) SystemException(message, innerException) HResult = ActiveBasic.AB_E_ARGUMENT End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] paramName 原因となった仮引数名 */ Sub ArgumentException(message As String, paramName As String) SystemException(message, Nothing) param = paramName HResult = ActiveBasic.AB_E_ARGUMENT End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 @param[in] paramName 原因となった仮引数名 */ Sub ArgumentException(message As String, paramName As String, innerException As Exception) SystemException(message, innerException) param = paramName HResult = ActiveBasic.AB_E_ARGUMENT End Sub Override Function Message() As String Dim sb = New System.Text.StringBuilder sb.Append(param).Append(": ").Append(Super.Message) Return sb.ToString End Function /*! @brief この例外の発生原因の仮引数名の取得 */ Virtual Function Param() As String Return param End Function Private param As String End Class /*! @brief NothingまたはNULLを受け付けない引数にそれらが渡されたことを表す例外 @author Egtra @date 2007/11/19 */ Class ArgumentNullException Inherits ArgumentException Public /*! @biref コンストラクタ */ Sub ArgumentNullException() ArgumentException("One or more arguments have Nothing or Null value.", "", Nothing) HResult = E_POINTER End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub ArgumentNullException(message As String) ArgumentException(message, "", Nothing) HResult = E_POINTER End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub ArgumentNullException(message As String, innerException As Exception) ArgumentException(message, "", innerException) HResult = E_POINTER End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] paramName 原因となった仮引数名 */ Sub ArgumentNullException(message As String, paramName As String) ArgumentException(message, paramName) HResult = E_POINTER End Sub End Class /*! @brief 規定された範囲を超える実引数が渡されたことを表す例外 @author Egtra @date 2007/11/19 */ Class ArgumentOutOfRangeException Inherits ArgumentException Public /*! @biref コンストラクタ */ Sub ArgumentOutOfRangeException() ArgumentException("One or more arguments ware out of range value.", "", Nothing) HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub ArgumentOutOfRangeException(message As String) ArgumentException(message, "", Nothing) HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub ArgumentOutOfRangeException(message As String, innerException As Exception) ArgumentException(message, "", innerException) HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] paramName 原因となった仮引数名 */ Sub ArgumentOutOfRangeException(message As String, paramName As String) ArgumentException(message, paramName, Nothing) HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] actualValue 問題となった仮引数の値 @param[in] paramName 原因となった仮引数名 */ Sub ArgumentOutOfRangeException(message As String, actualValue As Object, paramName As String) ArgumentException(message, paramName, Nothing) actualValueObject = actualValue HResult = ActiveBasic.AB_E_ARGUMENTOUTOFRANGE End Sub /*! @brief この例外の発生原因の実引数の値の取得 */ Virtual Function ActualValue() As Object Return actualValueObject End Function Override Function Message() As String If ActiveBasic.IsNothing(actualValueObject) Then Return Super.Message Else Dim sb = New System.Text.StringBuilder sb.Append(Param).Append(" = ").Append(actualValueObject) sb.Append(": ").Append(Super.Message) Return sb.ToString End If End Function Private actualValueObject As Object End Class /*! @brief 配列の範囲外の要素を読み書きしようとしたことを表す例外 @author Egtra @date 2007/11/19 */ Class IndexOutOfRangeException Inherits SystemException Public /*! @biref コンストラクタ */ Sub IndexOutOfRangeException() SystemException("The index was out of range value.", Nothing) HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub IndexOutOfRangeException(message As String) SystemException(message, Nothing) HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub IndexOutOfRangeException(message As String, innerException As Exception) SystemException(message, innerException) HResult = ActiveBasic.AB_E_INDEXOUTOFRANGE End Sub End Class /*! @brief 無効な操作を行おうとしたことを表す例外 @author Egtra @date 2007/11/19 */ Class InvalidOperationException Inherits SystemException Public /*! @biref コンストラクタ */ Sub InvalidOperationException() SystemException("The operation is invalid.", Nothing) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub InvalidOperationException(message As String) SystemException(message, Nothing) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub InvalidOperationException(message As String, innerException As Exception) SystemException(message, innerException) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub End Class /*! @brief 博されたオブジェクトを操作しようとしたことを表す例外 @author Egtra @date 2007/12/06 */ Class ObjectDisposedException Inherits InvalidOperationException Public /*! @biref コンストラクタ */ Sub ObjectDisposedException() InvalidOperationException("The object has been disposed.", Nothing) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub ObjectDisposedException(message As String) InvalidOperationException(message, Nothing) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub ObjectDisposedException(message As String, innerException As Exception) InvalidOperationException(message, innerException) HResult = ActiveBasic.AB_E_INVALIDOPERATION End Sub /*! @biref コンストラクタ @param[in] objectName 操作しようとしたオブジェクトの名前 @param[in] message エラーメッセージ */ Sub ObjectDisposedException(objectName As String, message As String) InvalidOperationException(message, Nothing) HResult = ActiveBasic.AB_E_INVALIDOPERATION objName = " object: " + objectName End Sub /*! @brief この例外が発生したとき、操作しようとしていたオブジェクトの名前を返す。 */ Function ObjectName() As String Return objName End Function Override Function ToString() As String ToString = Super.ToString() If Not ActiveBasic.IsNothing(objName) Then ToString += objName End If End Function Private objName As String End Class /*! @brief そのメソッド・関数ないし操作が実装されていないことを表す例外 @author Egtra @date 2007/11/19 */ Class NotImplementedException Inherits SystemException Public /*! @biref コンストラクタ */ Sub NotImplementedException() SystemException("Not implemented.", Nothing) HResult = E_NOTIMPL End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub NotImplementedException(message As String) SystemException(message, Nothing) HResult = E_NOTIMPL End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub NotImplementedException(message As String, innerException As Exception) SystemException(message, innerException) HResult = E_NOTIMPL End Sub End Class /*! @brief 対応していないメソッド・関数ないし操作を行おうとしたことを表す例外 @author Egtra @date 2007/11/19 */ Class NotSupportedException Inherits SystemException Public /*! @biref コンストラクタ */ Sub NotSupportedException() SystemException("This operation is not supported,", Nothing) HResult = ActiveBasic.AB_E_NOTSUPPORTED End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub NotSupportedException(message As String) SystemException(message, Nothing) HResult = ActiveBasic.AB_E_NOTSUPPORTED End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub NotSupportedException(message As String, innerException As Exception) SystemException(message, innerException) HResult = ActiveBasic.AB_E_NOTSUPPORTED End Sub End Class /*! @brief 実行しているプラットフォームで対応していないメソッド・関数ないし操作を行おうとしたことを表す例外 @author Egtra @date 2007/11/19 */ Class PlatformNotSupportedException Inherits NotSupportedException Public /*! @biref コンストラクタ */ Sub PlatformNotSupportedException() NotSupportedException("This operation is not supported in this platform.", Nothing) HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub PlatformNotSupportedException(message As String) NotSupportedException(message, Nothing) HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub PlatformNotSupportedException(message As String, innerException As Exception) NotSupportedException(message, innerException) HResult = ActiveBasic.AB_E_PLATFORMNOTSUPPORTED End Sub End Class /*! @brief 操作が取り止められたことを表す例外 @author Egtra @date 2007/11/19 @todo HResultの調査 */ Class OperationCanceledException Inherits SystemException Public /*! @biref コンストラクタ */ Sub OperationCanceledException() SystemException("The operation was canceled.", Nothing) End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub OperationCanceledException(message As String) SystemException(message, Nothing) End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub OperationCanceledException(message As String, innerException As Exception) SystemException(message, innerException) End Sub End Class /*! @brief メモリ不足例外 @author Egtra @date 2007/11/19 @todo HResultの調査 */ Class OutOfMemoryException Inherits SystemException Public /*! @biref コンストラクタ */ Sub OutOfMemoryException() SystemException("Out of memory.", Nothing) HResult = E_OUTOFMEMORY End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub OutOfMemoryException(message As String) SystemException(message, Nothing) HResult = E_OUTOFMEMORY End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub OutOfMemoryException(message As String, innerException As Exception) SystemException(message, innerException) HResult = E_OUTOFMEMORY End Sub End Class /*! @brief 算術演算例外 @author Egtra @date 2009/02/16 */ Class ArithmeticException Inherits SystemException Public /*! @biref コンストラクタ */ Sub ArithmeticException() SystemException("ArithmeticException", Nothing) HResult = ActiveBasic.AB_E_ARITHMETIC End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub ArithmeticException(message As String) SystemException(message, Nothing) HResult = ActiveBasic.AB_E_ARITHMETIC End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub ArithmeticException(message As String, innerException As Exception) SystemException(message, innerException) HResult = ActiveBasic.AB_E_ARITHMETIC End Sub End Class /*! @brief オーバーフロー例外 @author Egtra @date 2009/02/16 */ Class OverflowException Inherits ArithmeticException Public /*! @biref コンストラクタ */ Sub OverflowException() ArithmeticException("OverflowException", Nothing) HResult = ActiveBasic.AB_E_OVERFLOW End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ */ Sub OverflowException(message As String) ArithmeticException(message, Nothing) HResult = ActiveBasic.AB_E_OVERFLOW End Sub /*! @biref コンストラクタ @param[in] message エラーメッセージ @param[in] innerException 内部例外 */ Sub OverflowException(message As String, innerException As Exception) ArithmeticException(message, innerException) HResult = ActiveBasic.AB_E_OVERFLOW End Sub End Class End Namespace