Complete the code to ensure the finally block always executes.
try { Console.WriteLine("Try block"); } [1] { Console.WriteLine("Finally block"); }
The finally block always runs after the try block, regardless of exceptions.
Complete the code to catch exceptions and still run the finally block.
try { int x = 5 / 0; } [1] (DivideByZeroException ex) { Console.WriteLine("Caught exception"); } finally { Console.WriteLine("Always runs"); }
The catch block handles exceptions, and the finally block runs after that.
Fix the error in the code to ensure the finally block runs after the try-catch.
try { Console.WriteLine("Start"); } catch (Exception ex) { Console.WriteLine("Error"); } [1] { Console.WriteLine("Cleanup"); }
The finally block must be used to run code after try-catch regardless of exceptions.
Fill both blanks to complete the try-catch-finally structure correctly.
try { int[] arr = new int[2]; Console.WriteLine(arr[3]); } [1] (IndexOutOfRangeException ex) { Console.WriteLine("Index error"); } [2] { Console.WriteLine("Always runs"); }
The catch block handles the exception, and finally runs always after try-catch.
Fill all three blanks to create a try-catch-finally that throws and cleans up properly.
try { Console.WriteLine("Start"); throw new Exception("Error"); } [1] (Exception ex) { Console.WriteLine(ex.Message); [2]; } [3] { Console.WriteLine("Cleanup done"); }
The catch block catches the exception, throw rethrows it, and finally runs cleanup code.