在try catch中尝试catch的最佳实践是什么?

时间:2013-05-26 07:18:22

标签: c# try-catch

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

我执行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);
        }

2 个答案:

答案 0 :(得分:4)

您永远不会使用例外来控制程序的流程。

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 
}   

答案 1 :(得分:0)

单独设置try catch块,以便每个try catch块都遵循单一责任。在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);
            }
        }
    }