Do I have to break after throwing exception?

前端 未结 6 1663
予麋鹿
予麋鹿 2021-01-03 20:58

I\'m writing a custom class in C# and I\'m throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the c

6条回答
  •  借酒劲吻你
    2021-01-03 21:23

    When you throw an exception, the next code to get executed is any catch block that covers that throw within the method (if any) then, the finally block (if any). You can have a try, a try-catch, a try-catch-finally or a try-finally. Then, if the exception is not handled, re-thrown by a catch block or not caught at all, control is returned to the caller. For example, you will get "Yes1, Yes2, Yes3" from this code ...

    try
    {
        Console.WriteLine("Yes1");
        throw (new Exception());
        Console.WriteLine("No1");
    
    }
    catch
    {
        Console.WriteLine("Yes2");
        throw;
        Console.WriteLine("No2");
    }
    finally
    {
        Console.WriteLine("Yes3");
    }
    
    Console.WriteLine("No3");
    

提交回复
热议问题