2

XML ファイルを解析するプロセスがあります。

これは、パッケージ クラスで発生しています。

Package クラスには、オブジェクトを無効な状態に設定し、Package クラスで発生したエラーに関する詳細情報を取得する Delegate があります。

簡単にするために、パッケージに渡される filitem を示しています。

つまり `

foreach( var package in Packages)
{
try
{

    package.ProcessXml(fileitem.nextfile);

}
catch (CustomeErrorException ex)
{
    Logger.LogError(ex)
}
}

パッケージ内での検証は次のようになります

var Album = xml.Descendants()
    .Select(albumShards => new Album {
      Label = (string)albumShards.Descendants(TempAlbum.LabelLoc).FirstOrDefault() == "" ?
FailPackage("Error on label Load",Componets.Package,SubComp.BuildAlbum ) :  (string)albumShards.Descendants(TempAlbum.LabelLoc).FirstOrDefault()

この検証では、ラベルに "" が返されるかどうかを確認します...もしそうなら、エラー情報で Failpackage を呼び出し、例外を作成します

 protected override void FailPackage(string msg, LogItem logItem)
         {
             Valid = ProcessState.Bad;
             Logger.LogError(msg,logItem);
             throw CustomErrorException(msg, Logitem);

         }

含まれている try catch ブロックを介してキャプチャされる

私の懸念は、プログラムフローの例外を使用していることです...他にどのようにこの問題にアプローチする必要があるか、これは有効なパターンですか.

4

2 に答える 2

2

ProcessXml fails to do what its name implies in certain cases; these are error cases. Exceptions are for error handling, despite what the name implies.

One of the biggest misconceptions about exceptions is that they are for “exceptional conditions.” The reality is that they are for communicating error conditions.

Krzysztof Cwalina, Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries

In other words, you're in the right. :)

Read the chapter on exceptions in the above book for some excellent guidelines.

于 2011-12-19T01:21:09.120 に答える
0

エラーを ProcessState と共に置くことができます。

foreach( var package in Packages)
{
    package.ProcessXml(fileitem.nextfile);
    if(!package.Valid)
        Logger.LogError(package.Error)
}



var albumShards = xml.Descendants().FirstOrDefault();
if((string)albumShards.Descendants(TempAlbum.LabelLoc).FirstOrDefault() == "")
    return FailPackage("Error on label Load",Componets.Package,SubComp.BuildAlbum );

album = (string)albumShards.Descendants(TempAlbum.LabelLoc);


 protected override void FailPackage(string msg, LogItem logItem)
 {
     Valid = ProcessState.Bad;
     Logger.LogError(msg,logItem);
     Error = msg;
 }
于 2011-12-24T04:06:50.360 に答える