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

后端 未结 10 2647
陌清茗
陌清茗 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:18

    You could do something like this:

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

提交回复
热议问题