How does the try catch finally block work?

后端 未结 6 1439
耶瑟儿~
耶瑟儿~ 2021-02-06 20:35

In C#, how does a try catch finally block work?

So if there is an exception, I know that it will jump to the catch block and then jump to the finally block.

6条回答
  •  遥遥无期
    2021-02-06 21:23

    finally block always run , no matter what. just try this method

         public int TryCatchFinally(int a, int b)
        {
            try
            {
                int sum = a + b;
                if (a > b)
                {
                    throw new Exception();
                }
                else
                {
                    int rightreturn = 2;
                    return rightreturn;
                }
            }
            catch (Exception)
            {
                int ret = 1;
                return ret;
            }
            finally
            {
                int fin = 5;
            }
        }
    

提交回复
热议问题