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 5Create an integer variable called
index and set it to 1Use a
try block to divide 100 by numbers[index] and store the result in resultUse 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