Lösung: Aufruf des finally-Blocks
Completion requirements
- using System;
- using System.Collections.Generic;
- namespace LoesFinallyReturn
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("return i=" + test());
- Console.ReadKey();
- }
- static public int test()
- {
- int i = 1;
- try
- {
- throw new Exception("Test");
- }
- catch
- {
- return i; // i=1 wird zurückgegeben, aber vorher wird finally ausgeführt
- }
- finally
- {
- i++;
- Console.WriteLine("finally i="+i);
- }
- }
- }
- }
Ausgabe
finally i=2
return i=1
Die Ausgabe zeigt, dass zuerst der finally-Block ausgeführt, noch bevor die Methode mit return verlassen wird.
Last modified: Thursday, 19 September 2019, 11:24 AM