C#: Equivalent of the python try/catch/else block

后端 未结 10 2620
陌清茗
陌清茗 2021-02-15 10:53

In Python, there is this useful exception handling code:

try:
    # Code that could raise an exception
except Exception:
    # Exception handling
else:
    # Cod         


        
10条回答
  •  长情又很酷
    2021-02-15 11:16

    if (!IsReadOnly)
            {
                T newobj;
                bool Done;
                try
                {
                    newobj = DataPortal.Update(this);
                    List keys = new List(BasicProperties.Keys);
                    foreach (string key in keys)
                    {
                        BasicProperties[key] = newobj.BasicProperties[key];
                    }
                    Done = true;
                }
                catch (DataPortalException)
                {
                    // TODO: Implement DataPortal.Update() recovery mechanism
                    Done = false;
                }
                finally
                {
                    if (newobj != null && Done == false)
                    {
                        List keys = new List(BasicProperties.Keys);
                        foreach (string key in keys)
                        {
                            BasicProperties[key] = newobj.BasicProperties[key];
                        }
                    }
                }
            }
    

提交回复
热议问题