我在类 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
语句未捕获异常。我还尝试用 FormatException
替换 Exception
但没有成功。我做错了什么?
最佳答案
这段代码中必须有静音异常
if (CustomStringFunctions.IsGuid(productId))
{
//Do Something
}
您必须确保在发生异常时抛出异常(在“Do Something”中)
静音异常示例
Try
{
}
Catch(Exception ex)
{
//throw don't exist
}
关于c# - 代码未捕获 C# 中的 FormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12198100/