Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Finally Block Behavior
📖 Scenario: Imagine you are writing a program that reads a number from a list and divides 100 by that number. Sometimes the number might be zero, which causes an error. You want to make sure that no matter what happens, a message is always shown to say the operation is complete.
🎯 Goal: You will create a program that uses a try, catch, and finally block to handle division by zero errors and always print a completion message.
📋 What You'll Learn
Create an integer array called numbers with the values 10, 0, and 5
Create an integer variable called index and set it to 1
Use a try block to divide 100 by numbers[index] and store the result in result
Use a catch block to catch DivideByZeroException and print "Cannot divide by zero."
Use a finally block to print "Operation complete."
Print the result if no exception occurs
💡 Why This Matters
🌍 Real World
Handling errors safely is important in real programs to avoid crashes and to clean up resources.
💼 Career
Understanding <code>try-catch-finally</code> blocks is essential for writing robust C# applications in many software development jobs.
Progress0 / 4 steps
1
Create the numbers array
Create an integer array called numbers with the values 10, 0, and 5.
C Sharp (C#)
Hint
Use int[] numbers = {10, 0, 5}; to create the array.
2
Set the index variable
Create an integer variable called index and set it to 1.
C Sharp (C#)
Hint
Use int index = 1; to set the index.
3
Add try, catch, and finally blocks
Write a try block that divides 100 by numbers[index] and stores the result in an integer variable called result. Add a catch block to catch DivideByZeroException and print "Cannot divide by zero.". Add a finally block that prints "Operation complete.".
C Sharp (C#)
Hint
Use try, catch (DivideByZeroException), and finally blocks as shown.
4
Print the result
Add a line to print the value of result after the try-catch-finally blocks.
C Sharp (C#)
Hint
Use Console.WriteLine(result); to print the result.
Practice
(1/5)
1. What is the main purpose of the finally block in C# exception handling?
easy
A. To execute code regardless of whether an exception occurs or not
B. To catch exceptions thrown in the try block
C. To declare variables used in the try block
D. To stop the program when an exception occurs
Solution
Step 1: Understand the role of finally
The finally block runs after the try and catch blocks, no matter what happens.
Step 2: Identify its purpose
It is used to run cleanup code or important steps that must always execute, regardless of exceptions.
Final Answer:
To execute code regardless of whether an exception occurs or not -> Option A
Quick Check:
finally always runs [OK]
Hint: Remember: finally always runs, no matter what [OK]
Common Mistakes:
Confusing finally with catch block
Thinking finally only runs on exceptions
Believing finally can catch exceptions
2. Which of the following is the correct syntax for using a finally block in C#?
easy
A. try { } catch { } finally { }
B. try { } finally { } catch { }
C. try { } catch { }
D. finally { } try { } catch { }
Solution
Step 1: Recall correct order of blocks
In C#, the order is try, then catch (optional), then finally (optional).