C# 什么是最好的实践中尝试捕捉内尝试捕捉?

C# 什么是最好的实践中尝试捕捉内尝试捕捉?,c#,try-catch,C#,Try Catch,我的功能是在有预订请求时保存客户数据。有两个条件:即使客户数据尚未填写,也强制保存预订,或者使用已完成的客户数据保存预订 我执行try-catch并向用户提出异常,以防程序发现该客户不存在,从而确认用户是否想要创建新的客户数据。如果是,打开新窗体,如果不是,则强制程序保存 在强制程序保存的内部,我希望捕获其他异常(如果存在) 目前,我正在这样做 try { booking = new Booking(); booki

我的功能是在有预订请求时保存客户数据。有两个条件:即使客户数据尚未填写,也强制保存预订,或者使用已完成的客户数据保存预订

我执行try-catch并向用户提出异常,以防程序发现该客户不存在,从而确认用户是否想要创建新的客户数据。如果是,打开新窗体,如果不是,则强制程序保存

在强制程序保存的内部,我希望捕获其他异常(如果存在)

目前,我正在这样做

        try
        {
            booking = new Booking();
            booking.RoomID = roomID;
            booking.Tel = txtTel.Text;
            booking.PersonalID = txtId.Text;
            booking.Name = txtBookBy.Text;
            booking.CheckinDate = dtpCheckin.Value;
            booking.CheckoutDate = dtpCheckin.Value.AddDays(Convert.ToDouble(cbNight.SelectedItem));
            mng.SaveBooking(booking, false);
            if (MessageBox.Show("Data is saved") == DialogResult.OK)
            {
                this.Close();
            }

        }
        catch (NewCustomerException ex)
        {
            DialogResult dialog = MessageBox.Show("Customer doesn't exist in database. Do you want to create new customer?", "Please confirm", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                String param = txtBookBy.Text + "," + txtTel.Text + "," + txtId.Text;
                CustomerForm oForm = new CustomerForm(param);
                oForm.Show();
            }
            else if (dialog == DialogResult.No)
            {
                try
                {
                    mng.SaveBooking(booking, true);
                }
                catch (Exception ex1)
                { 
                    MessageBox.Show(ex1.Message);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

您从不使用异常来控制程序流

mng.SaveBooking(booking,false)
应返回true/false,以向调用代码发出客户不存在的信号

最好编写一个只返回此信息的方法,然后使用该信息编写以下代码

try
{
    // Check if customer already registered
    if(mng.CustomerExists(CustomerKey))
    {
        mng.SaveBooking(booking, false);
        if (MessageBox.Show("Data is saved") == DialogResult.OK)
        {
            this.Close();
        }
    }
    else
    {
        DialogResult dialog = MessageBox.Show("Customer doesn't exist in database. " + 
             "Do you want to create new customer?", "Please confirm", MessageBoxButtons.YesNo);
        if (dialog == DialogResult.Yes)
        {
          .....
        }
        else
        {
          .....
        }
    }
}
catch(Exception ex)
{
   ... somthing unexpected here 
}   

您从不使用异常来控制程序流

mng.SaveBooking(booking,false)
应返回true/false,以向调用代码发出客户不存在的信号

最好编写一个只返回此信息的方法,然后使用该信息编写以下代码

try
{
    // Check if customer already registered
    if(mng.CustomerExists(CustomerKey))
    {
        mng.SaveBooking(booking, false);
        if (MessageBox.Show("Data is saved") == DialogResult.OK)
        {
            this.Close();
        }
    }
    else
    {
        DialogResult dialog = MessageBox.Show("Customer doesn't exist in database. " + 
             "Do you want to create new customer?", "Please confirm", MessageBoxButtons.YesNo);
        if (dialog == DialogResult.Yes)
        {
          .....
        }
        else
        {
          .....
        }
    }
}
catch(Exception ex)
{
   ... somthing unexpected here 
}   

制作单独的试捕块,以便每个试捕块都遵循单一责任。在catch块中,您可以管理它引发异常的语句,并且可以在下一个try-catch块中使用该manage语句结果

    try {
        booking = new Booking();
        booking.RoomID = roomID;
        booking.Tel = txtTel.Text;
        booking.PersonalID = txtId.Text;
        booking.Name = txtBookBy.Text;
        booking.CheckinDate = dtpCheckin.Value;
        booking.CheckoutDate = dtpCheckin.Value.AddDays(Convert.ToDouble(cbNight.SelectedItem));
        mng.SaveBooking(booking, false);
        if (MessageBox.Show("Data is saved") == DialogResult.OK) {
            this.Close();
        }

    }
    catch (NewCustomerException ex) {
        DialogResult dialog = MessageBox.Show("Customer doesn't exist in database. Do you want to create new customer?", "Please confirm", MessageBoxButtons.YesNo);

    }

    if (dialog == DialogResult.Yes) {
            String param = txtBookBy.Text + "," + txtTel.Text + "," + txtId.Text;
            CustomerForm oForm = new CustomerForm(param);
            oForm.Show();
        }
        else if (dialog == DialogResult.No) {
            try {
                mng.SaveBooking(booking, true);
            }
            catch (Exception ex1) { 
                MessageBox.Show(ex1.Message);
            }
        }
    }

制作单独的试捕块,以便每个试捕块都遵循单一责任。在catch块中,您可以管理它引发异常的语句,并且可以在下一个try-catch块中使用该manage语句结果

    try {
        booking = new Booking();
        booking.RoomID = roomID;
        booking.Tel = txtTel.Text;
        booking.PersonalID = txtId.Text;
        booking.Name = txtBookBy.Text;
        booking.CheckinDate = dtpCheckin.Value;
        booking.CheckoutDate = dtpCheckin.Value.AddDays(Convert.ToDouble(cbNight.SelectedItem));
        mng.SaveBooking(booking, false);
        if (MessageBox.Show("Data is saved") == DialogResult.OK) {
            this.Close();
        }

    }
    catch (NewCustomerException ex) {
        DialogResult dialog = MessageBox.Show("Customer doesn't exist in database. Do you want to create new customer?", "Please confirm", MessageBoxButtons.YesNo);

    }

    if (dialog == DialogResult.Yes) {
            String param = txtBookBy.Text + "," + txtTel.Text + "," + txtId.Text;
            CustomerForm oForm = new CustomerForm(param);
            oForm.Show();
        }
        else if (dialog == DialogResult.No) {
            try {
                mng.SaveBooking(booking, true);
            }
            catch (Exception ex1) { 
                MessageBox.Show(ex1.Message);
            }
        }
    }

通常,您应该使您的
try catch
-块尽可能短,并避免嵌套。除非您在构造函数中执行关键操作,否则在“try-catch”中创建对象通常是不必要的。嵌套的try/catch通常意味着其中有另一个方法试图退出。您通常应尽可能缩短try-catch-块并避免嵌套。在“try-catch”中创建对象通常是不必要的,除非您正在构造函数中执行关键操作。嵌套的try/catch通常意味着其中有另一个方法试图退出。