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

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

提交回复
热议问题