Asp.net Web表单中的错误处理

Asp.net Web表单中的错误处理,asp.net,error-handling,Asp.net,Error Handling,我试图解决数据访问层中的一个错误,该错误将返回值为-1的int。见下文: protected void FolderBtn_Click(object sender, EventArgs e) { if (Page.IsValid) { try { DocsDALC dalc = new DocsDALC(); // Updating two tables here - Folders and Fol

我试图解决数据访问层中的一个错误,该错误将返回值为-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方法