2

クラスに次のメソッドがありますProductServices

public bool IsDownloadAllowed(int Id, string productId)
{
  if (CustomStringFunctions.IsGuid(productId))
  {
     //Do Something
  }
  else
  {
     throw new FormatException("The Guid must be a valid Guid!");
  }
}

次の一連の手順でメソッドを使用する場合:

var _productServices = new ProductServices();
try
{
   var flag = _productServices.IsDownloadAllowed(Id, productId);
   //do something
}

catch (Exception e)
{
   //handle exception
}

例外はcatchステートメントによってキャッチされません。私も運が悪いExceptionと交換しようとしました。FormatException私は何を間違っていますか?

4

2 に答える 2

1

このコードにはミュート例外が必要です

if (CustomStringFunctions.IsGuid(productId))
  {
     //Do Something
  }

発生したときに例外をスローすることを確認する必要があります(何かをする際に)

ミュート例外のサンプル

Try
{

}
Catch(Exception ex)
{
   //throw don't exist
}
于 2012-08-30T13:35:13.113 に答える
1

あなたのコードは正しいようです。問題の考えられる説明は、CustomStringFunctions.IsGuidが誤って返さtrueれているため\\do something、例外をスローするブランチではなくブランチが実行されていることです。

于 2012-08-30T14:35:27.710 に答える