gpt4 book ai didi

c# - 如何将 Oracle 异常消息与字符串进行比较?

转载 作者:行者123 更新时间:2023-11-30 19:54:36 24 4
gpt4 key购买 nike

我是 C# 的新手,我正在为非常基本的东西而苦苦挣扎......例如,我现在正在尝试比较一个异常,我知道如果我使用 Response.Write(ex.Message)。但是,在下面的代码块中,ex.Message 与刚刚提供的字符串之间的比较失败,它返回我在 else 子句中放入的未处理异常...我应该如何比较异常与字符串?

catch (Exception ex)
{
if (ex.Message == "ORA-28007: the password cannot be reused")
{
Response.Write(ex.Message);
// TODO : Correct the exception to be presented in the popup instead of the same page.
// display = "The password cannot be reused! Pick a new one.";
// ClientScript.RegisterStartupScript(this.GetType(),
// "Error.", "alert('" + display + "');", true);
}
else
{
Response.Write("Unhandled exception: " + ex.Message);
}
}

最佳答案

如果您使用的是 Oracle Data Provider for .NET您可以捕获 OracleException 而不是 Exception 并通过查看 Errors 属性获取更多详细信息,该属性给出了 OracleError 的列表对象:

catch(OracleException oex)
{
foreach (OracleError error in oex.Errors)
{
Console.WriteLine("Error Message: " + error.Message);
Console.WriteLine("Error Source: " + error.Source);

if(error.Number == 28007)
{
// do specific stuff
}
}
}

关于c# - 如何将 Oracle 异常消息与字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306678/

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