1

我是从数据库输入信息的 LINQ。我设置了 try.catch 块来捕获这些异常。但是我相信我遇到了一个痛点,我试图查看消息是什么,但它只是绕过向我打印消息并直接进入错误页面。这是我到目前为止的代码示例。我很想得到一些关于为什么这看起来如此奇怪的意见。

 private void CreateEntry()
    {
        var date = DateTime.Today;
        var version = (from v in house.StayLateVersions
                       where v.Active
                       select v).FirstOrDefault();

        if (version == null)
        {
            throw new NullReferenceException();
        }

        //Try to create an entry for the database.  Upon failure, sends the exception to ThrowDbError();

        try
        {
            ResidenceHallInspection rhi = new ResidenceHallInspection();

            rhi.versionId = version.id;
            rhi.submitDate = DateTime.Now;
            rhi.CheckInOrOut = ddlCheck.SelectedItem.Text;
            rhi.Id = txtId.Text;
            rhi.FirstName = txtFirstName.Text;
            rhi.MiddleName = txtMiddleName.Text;
            rhi.LastName = txtLastName.Text;
            rhi.Walls = chbxWalls.SelectedItem.Text;
            rhi.Windows = chbxWindows.SelectedItem.Text;
            rhi.Blinds = chbxBlinds.SelectedItem.Text;
            rhi.Couch = chbxCouch.SelectedItem.Text;
            rhi.CommonRoomCouch = chbxCRCouch.SelectedItem.Text;
            rhi.CommonRoomChair = chbxCRChair.SelectedItem.Text;
            rhi.Doors = chbxDoors.SelectedItem.Text;
            rhi.Carpet = chbxCarpet.SelectedItem.Text;
            rhi.Ceiling = chbxCeiling.SelectedItem.Text;
            rhi.CommonRoomCounter = chbxCRCounter.SelectedItem.Text;
            rhi.Cabinet = chbxCabinet.SelectedItem.Text;
            rhi.Phone = chbxPhone.SelectedItem.Text;
            rhi.Bed = chbxBed.SelectedItem.Text;
            rhi.Desk = chbxDesk.SelectedItem.Text;
            rhi.DeskChairs = chbxDeskChair.SelectedItem.Text;
            rhi.Tub = chbxTub.SelectedItem.Text;
            rhi.Vanity = chbxVanity.SelectedItem.Text;
            rhi.Notes = txtNotes.Text;
            rhi.Building = txtResHall.Text;
            rhi.ApartmentNumber = txtSuitNo.Text;
            rhi.BedSpace = txtBedSpace.Text;

            house.AddToResidenceHallInspections(rhi);
            house.SaveChanges();

        }
        catch (Exception oe)
        {
            ThrowDbError(oe);
            Response.Write(oe.InnerException);
        }
    }

    /*=================================================*/
    /*Possible Errors                                  */
    /*=================================================*/

    private void ThrowDbError(Exception oe)
    {
        Response.Write(oe.Source);
        house.Dispose();
        Session.Contents.Add("FormException", oe);
        Response.Redirect("/Database-Error/", true);
    }
4

1 回答 1

0

发生这种情况的最可能原因是您version在 try/catch 块之外运行数据库查询。此数据库访问代码中的任何异常都不会由您上面显示的代码处理。

尝试扩展您的try块以包括数据库访问代码:

var version = (from v in house.StayLateVersions
                   where v.Active
                   select v).FirstOrDefault();

if (version == null)
{
    throw new NullReferenceException();
}

看看这次错误是否被捕获。

于 2013-02-25T21:21:22.683 回答