How does the try catch finally block work?

后端 未结 6 1446
耶瑟儿~
耶瑟儿~ 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:12

    Yes the finally clause gets exeucuted if there is no exception. Taking an example

         try
            {
                int a = 10;
                int b = 20;
                int z = a + b;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.WriteLine("Executed");
            }
    

    So here if suppose an exception occurs also the finally gets executed.

提交回复
热议问题