c# - 代码未捕获 C# 中的 FormatException

标签 c# exception try-catch formatexception

我在类 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/

相关文章:

c# - 修改现有程序以更好地处理异常

.net - Control.EndInvoke 为异常重置调用堆栈

c# - N 层应用程序中的多个 DbContext

java - 如何使用 Java 中基于信号处理的机制将 JNI 崩溃捕获为异常

c# - 获取类属性长度

php - 在 PHP 中使用 odbc_exec 时使用 try-catch 语句的意外行为

R - tryCatch - 使用最后一次迭代索引重新启动 for 循环

java - 安全的 SimpleDateFormat 解析

c# - 如何模拟 WCF 服务?

c# - 断开连接后如何重新连接到 SQL Server?