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

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

    Just put your "else" block before the catch. Then, it will only execute if code execution reaches that point:

    try
    {
        fee();
        fi();
        foe();
        fum();
    
        /// put your "else" stuff here. 
        /// It will only be executed if fee-fi-foe-fum did not fail.
    }
    catch(Exception e)
    {
        // handle exception
    }
    

    Given that, I fail to see the use of try..catch...else unless there's something vital missing from the OP's description.

提交回复
热议问题