首页 > 其他分享 >控制不能离开Finally子句主体

控制不能离开Finally子句主体

时间:2023-02-10 10:48:16浏览次数:44  
标签:主体 searchModal UIModelData updateModal Finally uIModelData 子句 model IngredientCN

1。在try{}catch{}finally{}的结构中不可以将返回语句放置在finally的主体当中2.如果在catch{}中有向上一级从新抛出异常操作,在finally{}之后的语句将不会执行

3.如果在catch{}中没有向上一级从新抛出异常操作,finally{}之后的语句可以执行。

啥意思呢?

这样写不行

 

public UIModelData<UI_IngredientTable> Update(UI_IngredientTable model)
        {
            UIModelData<UI_IngredientTable> uIModelData = null;
            try
            {
                uIModelData = new UIModelData<UI_IngredientTable> { };
                int total = GetTotal(i => i.IngredientCNName == model.IngredientCNName && i.IngredientID != model.IngredientID);
                if (total > 0)
                {
                    uIModelData.status = 7;
                    uIModelData.suc = false;
                    uIModelData.remark = string.Format("辅料中文名称重复,录入失败");
                }
                else
                {
                    IngredientTable updateModal = Mapper.Map<IngredientTable>(model);
                    IngredientTable searchModal = contextItem.FirstOrDefault(i => i.IngredientID == updateModal.IngredientID);
                    searchModal.IngredientAlias = updateModal.IngredientAlias;

                    searchModal.CPName = updateModal.CPName;
                    searchModal.EPName = updateModal.EPName;
                    searchModal.JPName = updateModal.JPName;
                    searchModal.USPName = updateModal.USPName;


                    searchModal.IngredientCNName = updateModal.IngredientCNName;
                    searchModal.IngredientShortName = updateModal.IngredientShortName;
                    searchModal.LastUpdateTime = updateModal.LastUpdateTime;
                    yuhongxiadataEntities.SaveChanges();
                    uIModelData.status = 6;
                    uIModelData.suc = true;
                    uIModelData.Data = model;
                }
            }
            catch (Exception ex)
            {
                uIModelData = new UIModelData<UI_IngredientTable> { status = 7, suc = false, remark = ex.Message };


            }
finnaly{
return uIModelData;

}
 }

 

 

改成这样

   public UIModelData<UI_IngredientTable> Update(UI_IngredientTable model)

        {
            UIModelData<UI_IngredientTable> uIModelData = null;
            try
            {
                uIModelData = new UIModelData<UI_IngredientTable> { };
                int total = GetTotal(i => i.IngredientCNName == model.IngredientCNName && i.IngredientID != model.IngredientID);
                if (total > 0)
                {
                    uIModelData.status = 7;
                    uIModelData.suc = false;
                    uIModelData.remark = string.Format("辅料中文名称重复,录入失败");
                }
                else
                {
                    IngredientTable updateModal = Mapper.Map<IngredientTable>(model);
                    IngredientTable searchModal = contextItem.FirstOrDefault(i => i.IngredientID == updateModal.IngredientID);
                    searchModal.IngredientAlias = updateModal.IngredientAlias;

                    searchModal.CPName = updateModal.CPName;
                    searchModal.EPName = updateModal.EPName;
                    searchModal.JPName = updateModal.JPName;
                    searchModal.USPName = updateModal.USPName;


                    searchModal.IngredientCNName = updateModal.IngredientCNName;
                    searchModal.IngredientShortName = updateModal.IngredientShortName;
                    searchModal.LastUpdateTime = updateModal.LastUpdateTime;
                    yuhongxiadataEntities.SaveChanges();
                    uIModelData.status = 6;
                    uIModelData.suc = true;
                    uIModelData.Data = model;
                }
            }
            catch (Exception ex)
            {
                uIModelData = new UIModelData<UI_IngredientTable> { status = 7, suc = false, remark = ex.Message };


            }
finnaly{
}
return uIModelData; }

 

标签:主体,searchModal,UIModelData,updateModal,Finally,uIModelData,子句,model,IngredientCN
From: https://www.cnblogs.com/sexintercourse/p/17108076.html

相关文章