gpt4 book ai didi

c# - 如何区分 InvalidOperationException 异常类型?

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:21 24 4
gpt4 key购买 nike

如何检测 InvalidOperationException 类型

这是内部异常消息:

System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

我需要准确地检测到这种类型的异常来处理它。我可以知道它的 HResult 编号或异常代码吗?还是其他方式?

最佳答案

这段代码可能有帮助

try
{
//your code here...
}
catch (Exception exception) when (exception.InnerException is InvalidOperationException)
{
var exceptionMessage = "ExecuteNonQuery requires an open and available Connection";
if (exception.Message.Contains(exceptionMessage) || exception.InnerException.Message.Contains(exceptionMessage))
{
//handle it...
}
}

关于c# - 如何区分 InvalidOperationException 异常类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59019614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com