例外ビルダ メソッドを使用します。一般に、クラスはクラス実装内の複数の位置で同一の例外をスローします。コードが長くなることを防ぐため、例外を作成して返すヘルパ メソッドを使用します。たとえば、次のように指定します。
Class File
Private fileName As String
Public Function Read(bytes As Integer) As Byte()
If Not ReadFile(handle, bytes) Then
Throw NewFileIOException()
End If
End Function 'Read
Function NewFileIOException() As FileException
Dim description As String = __unknown ' Build localized string, including fileName.
Return New FileException(description) '
End Function 'NewFileIOException
End Class 'File
class File {
string fileName;
public byte[] Read(int bytes) {
if (!ReadFile(handle, bytes))
throw NewFileIOException();
}
FileException NewFileIOException() {
string description = // Build localized string, including fileName.
return new FileException(description);
}
}