gpt4 book ai didi

asp.net - Web表单中的错误处理

转载 作者:行者123 更新时间:2023-12-03 08:12:50 25 4
gpt4 key购买 nike

我正在尝试解决我的数据访问层中的错误,该错误将返回值-1的int。见下文:

protected void FolderBtn_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
DocsDALC dalc = new DocsDALC();

// Updating two tables here - Folders and FolderAccess tables
// - as an ADO.NET transaction
int folderID = dalc.CreateFolder(...);

if (folderID > 0)
{
Response.Redirect(Request.Url.ToString(), false);
// Re-construct this to include newly-created folderID
}
else
{
// How do I throw error from here?
}
}
catch (Exception ex)
{
HandleErrors(ex);
}
}
}

如果数据层返回-1,如何从try块中引发错误?

最佳答案

如下所示-但是,由于您正在捕获错误,因此,如果知道这是一个问题,最好调用HandleErrors方法的重载,该方法可以传入定义问题的字符串,而不是抛出异常(这样做会很昂贵)。

如果仍然要抛出异常:

        if (folderID > 0)
{
Response.Redirect(Request.Url.ToString(), false);
// Re-construct this to include newly-created folderID
}
else
{
throw new Exception("Database returned -1 from CreateFolder method");
}

可能的替代方法:
        if (folderID > 0)
{
Response.Redirect(Request.Url.ToString(), false);
// Re-construct this to include newly-created folderID
}
else
{
HandleErrors("Database returned -1 from CreateFolder method");
}

当然有了重载的HandleErrors方法。

关于asp.net - Web表单中的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527828/

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