如何在不退出循环的情况下处理异常?

时间:2013-10-21 11:11:12

标签: c# asp.net exception loops constraints

我有以下问题:

我尝试插入multiple employee data,但是员工数据应该只插入一次,但是如果我选择多个员工并且之前已经插入了其中一个,那么根据数据库约束将引发错误。

我的问题是(最佳实践):如何处理此异常,我想继续插入未重复的员工而不退出我的循环因为该异常。


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

4 个答案:

答案 0 :(得分:1)

将您的try / catch移至仅适用于obj.InsertReward();行。另外,请勿捕获Exception,只捕获insertReward抛出的特定异常。

因此代码最终可能会出现:

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

答案 1 :(得分:1)

你应该把try放在循环中:

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

答案 2 :(得分:1)

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

并且不要忘记在填写obj期间可能有例外

答案 3 :(得分:1)

确保你的try catch没有封装for循环 这样:

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

应该是:

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

或者如果在for循环中没有抛出异常,只需在开始循环之前捕获或在启动循环后尝试。