1

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

如果程序发现该客户不存在以确认用户是否想要创建新的客户数据,我会尝试捕获并向用户提出异常。如果是,则打开新表单,如果不是,则强制程序保存。

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

目前,我正在这样做。

        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);
        }
4

2 回答 2

4

您永远不会使用异常来控制程序的流程。

mng.SaveBooking(booking, 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 
}   
于 2013-05-26T07:21:49.463 回答
0

制作单独的 try catch 块,以便每个 try catch 块都遵循单一职责。在 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);
            }
        }
    }
于 2018-04-03T10:39:02.817 回答