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

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

    Barbaric solution: create an Else class derived from Exception, throw an instance of it at the end of the try block, and use catch (Else) {...} to handle the other stuff.

    I feel so dirty.

    0 讨论(0)
  • 2021-02-15 11:00

    I would prefer to see the rest of the code outside the try/catch so it is clear where the exception you are trying to catch is coming from and that you don't accidentally catch an exception that you weren't trying to catch.

    I think the closest equivalent to the Python try/catch/else is to use a local boolean variable to remember whether or not an exception was thrown.

    bool success;
    
    try
    {
        foo();
        success = true;
    }
    catch (MyException)
    {
        recover();
        success = false;
    }
    
    if (success)
    {
        bar();
    }
    

    But if you are doing this, I'd ask why you don't either fully recover from the exception so that you can continue as if there had been success, or else fully abort by returning an error code or even just letting the exception propagate to the caller.

    0 讨论(0)
  • 2021-02-15 11:09

    that would be the empty statement like hits

    try 
    { 
        somethingThatCanThrow(); 
    } 
    catch(Exception ex) 
    { 
        LogException(ex); 
        return;
    } 
    ContinueFlow();
    
    0 讨论(0)
  • 2021-02-15 11:09

    C# does not have such a concept, so you are just left with three options,

    • put the else code inside the try.
    • put the else code outside the try catch block, use a local variable to indicate success or failure, and an if block around your else code.
    • put the else code in the finally block, use a local variable to indicate success or failure, and an if block arount you else code.
    0 讨论(0)
  • 2021-02-15 11:11

    This will might get downvoted but doesn't c# have goto(note I have almost no c# knowledge so I have no idea if this works).

    what about something like

    try 
    { 
    ...
    } 
    catch(Exception ex) 
    { 
    ...
    goto Jump_past_tryelse
    } 
    ...//Code to execute if the try block DID NOT fail
    
    Jump_past_tryelse:
    ...
    
    0 讨论(0)
  • 2021-02-15 11:16
    if (!IsReadOnly)
            {
                T newobj;
                bool Done;
                try
                {
                    newobj = DataPortal.Update<T>(this);
                    List<string> keys = new List<string>(BasicProperties.Keys);
                    foreach (string key in keys)
                    {
                        BasicProperties[key] = newobj.BasicProperties[key];
                    }
                    Done = true;
                }
                catch (DataPortalException)
                {
                    // TODO: Implement DataPortal.Update<T>() recovery mechanism
                    Done = false;
                }
                finally
                {
                    if (newobj != null && Done == false)
                    {
                        List<string> keys = new List<string>(BasicProperties.Keys);
                        foreach (string key in keys)
                        {
                            BasicProperties[key] = newobj.BasicProperties[key];
                        }
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题