user2335149
user2335149

Reputation: 75

What is the best practice for try catch inside try catch?

My function is saving a customer data when there is a booking request. There are 2 conditions : force to save booking even if customer data is not filled yet or save booking with completed customer data.

I do the try catch and raise the exception to user in case that the program found that this customer doesn't exist to confirm that user wants to create new customer data or not. If yes, open new form and if not, force the program to save.

Inside forcing the program to save, I want to catch other exceptions if exist.

Currently, I'm doing like this.

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

Upvotes: 1

Views: 5824

Answers (2)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 1085

Make separate try catch block so that every try catch block follow single responsibility. In catch block you can manage the statement for which it throw exception and you can use that manage statement result in the next try catch block.

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

Upvotes: 0

Steve
Steve

Reputation: 216302

You never use exceptions to control the flow of your program.

The mng.SaveBooking(booking, false); should return true/false to signal the calling code that a customer doesn't exist.

Better to write a method that gives you back just this information and then use that info to write the following code

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 
}   

Upvotes: 4

Related Questions