0
0
C Sharp (C#)programming~10 mins

Finally block behavior in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to ensure the finally block always executes.

C Sharp (C#)
try {
    Console.WriteLine("Try block");
} [1] {
    Console.WriteLine("Finally block");
}
Drag options to blanks, or click blank then click option'
Aelse
Bcatch
Cwhen
Dfinally
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'catch' instead of 'finally' causes the block to run only on exceptions.
Using 'else' or 'when' are invalid here.
2fill in blank
medium

Complete the code to catch exceptions and still run the finally block.

C Sharp (C#)
try {
    int x = 5 / 0;
} [1] (DivideByZeroException ex) {
    Console.WriteLine("Caught exception");
} finally {
    Console.WriteLine("Always runs");
}
Drag options to blanks, or click blank then click option'
Ausing
Bfinally
Ccatch
Dthrow
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finally' instead of 'catch' to handle exceptions.
Using 'throw' or 'using' here is incorrect.
3fill in blank
hard

Fix the error in the code to ensure the finally block runs after the try-catch.

C Sharp (C#)
try {
    Console.WriteLine("Start");
} catch (Exception ex) {
    Console.WriteLine("Error");
} [1] {
    Console.WriteLine("Cleanup");
}
Drag options to blanks, or click blank then click option'
Afinally
Bcatch
Cthrow
Delse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'catch' again instead of 'finally'.
Using 'throw' or 'else' causes syntax errors.
4fill in blank
hard

Fill both blanks to complete the try-catch-finally structure correctly.

C Sharp (C#)
try {
    int[] arr = new int[2];
    Console.WriteLine(arr[3]);
} [1] (IndexOutOfRangeException ex) {
    Console.WriteLine("Index error");
} [2] {
    Console.WriteLine("Always runs");
}
Drag options to blanks, or click blank then click option'
Acatch
Bfinally
Cthrow
Dusing
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'catch' and 'finally' keywords.
Using 'throw' or 'using' in these positions.
5fill in blank
hard

Fill all three blanks to create a try-catch-finally that throws and cleans up properly.

C Sharp (C#)
try {
    Console.WriteLine("Start");
    throw new Exception("Error");
} [1] (Exception ex) {
    Console.WriteLine(ex.Message);
    [2];
} [3] {
    Console.WriteLine("Cleanup done");
}
Drag options to blanks, or click blank then click option'
Acatch
Bthrow
Cfinally
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'throw' causes the exception to be swallowed.
Using 'return' inside catch changes flow incorrectly.
Misplacing 'finally' block.