Anyname Donotcare
Anyname Donotcare

Reputation: 11423

How to handle the exception without exit the loop?

I have the following problem :

I try to insert multiple employee data , but the employee data should be inserted just once , but what if i select multiple employees and one of them has been inserted before ,according to the database constraint an error will be thrown .

My question is (best practice): How to handle this exception where, i want to resume inserting the unrepeated employees without exit my loop because of that exception .


protected void ibtn_save_detail_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Fill_Form();
                RewardDetails obj = new RewardDetails();
                var collection = ddl_employees.CheckedItems;
                for (int i = 0; i < collection.Count; i++)
                {
                    obj.Emp_num = int.Parse(collection[i].Value);
                    obj.Req_ser = int.Parse(reqSer);
                    obj.Req_year = int.Parse(reqYear);
                    DataTable dt = Utilities.GetDep(obj.Emp_num);
                    obj.Main_code = int.Parse(dt.Rows[0]["dep_code"].ToString());
                    obj.Year = int.Parse(dt.Rows[0]["dep_year"].ToString());
                    obj.Dep_name = dt.Rows[0]["dep_name"].ToString();

                    string res = obj.InsertReward();//exception in case of repetition .

                    if (!string.IsNullOrEmpty(res))
                    {

                        div_detail_result.Visible = true;
                        SetMessage("");

                    }
                    else
                    {
                        SetMessage("Adding the employee has been done :" + collection[i].Text.Trim());
                    }
                }
                BindDetailsGV(obj.Req_ser, obj.Req_year);
                ddl_employees.ClearCheckedItems();

            }
            catch (Exception ee)
            {
                SetMessage("Error,this employee has been added before.");
                ddl_employees.ClearCheckedItems();
            }
        }

Upvotes: 1

Views: 153

Answers (4)

David Arno
David Arno

Reputation: 43264

Move your try/catch to apply only to the obj.InsertReward(); line. Also, don't catch Exception, just the specific exception thrown by insertReward.

So the code might end up something like:

protected void ibtn_save_detail_Click(object sender, ImageClickEventArgs e)
{
    Fill_Form();
    RewardDetails obj = new RewardDetails();
    var collection = ddl_employees.CheckedItems;
    for (int i = 0; i < collection.Count; i++)
    {
        obj.Emp_num = int.Parse(collection[i].Value);
        ...

        string res;
        try
        {
            res = obj.InsertReward();
            // stuff to do if added
        }
        catch (RewardExistsException)
        {
            // stuff to do if already exists
        }
    }
}

Upvotes: 1

Vincent
Vincent

Reputation: 1557

make sure your try catch does not encapsulate the for loop so:

try
{
   for(...){}
}
catch(Exception){  }

should be:

for(...)
{
    try{  }
    Catch{  }
}

or if the exception is not thrown inside the for loop simply catch before you start the loop or try after you start the loop.

Upvotes: 1

Redwan
Redwan

Reputation: 758

try{
string res = obj.InsertReward();
}
catch(Exception ex){
    continue; // or rethrow exception in some condions
}

And don't forget that there could be exceptions during filling obj

Upvotes: 1

Szymon
Szymon

Reputation: 43023

You should put try inside the loop:

for (int i = 0; i < collection.Count; i++)
{
    try
            {
                obj.Emp_num = int.Parse(collection[i].Value);
                obj.Req_ser = int.Parse(reqSer);
                obj.Req_year = int.Parse(reqYear);
                DataTable dt = Utilities.GetDep(obj.Emp_num);
                obj.Main_code = int.Parse(dt.Rows[0]["dep_code"].ToString());
                obj.Year = int.Parse(dt.Rows[0]["dep_year"].ToString());
                obj.Dep_name = dt.Rows[0]["dep_name"].ToString();

                string res = obj.InsertReward();//exception in case of repetition .

                if (!string.IsNullOrEmpty(res))
                {

                    div_detail_result.Visible = true;
                    SetMessage("");

                }
                else
                {
                    SetMessage("Adding the employee has been done :" + collection[i].Text.Trim());
                }
            }
    catch (Exception ee)
        {
            // your exception code
        }
}

Upvotes: 1

Related Questions