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 回答