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
Using the Finally Block in Java
π Scenario: Imagine you are writing a simple Java program that reads a number from the user and divides 100 by that number. Sometimes the user might enter zero, which causes an error. You want to make sure that a message is always shown at the end, no matter what happens.
π― Goal: You will create a Java program that uses a try, catch, and finally block. The program will handle division by zero errors and always print a final message.
π What You'll Learn
Create an integer variable called number with the value 0
Create a try block that divides 100 by number
Create a catch block that catches ArithmeticException and prints "Cannot divide by zero"
Create a finally block that prints "End of program"
π‘ Why This Matters
π Real World
In real programs, you often need to handle errors like dividing by zero or missing files. The <code>finally</code> block helps you clean up or show messages no matter what happens.
πΌ Career
Understanding <code>try</code>, <code>catch</code>, and <code>finally</code> blocks is important for writing reliable Java applications, which is a key skill for many programming jobs.
Progress0 / 4 steps
1
DATA SETUP: Create the variable number
Create an integer variable called number and set it to 0.
Java
Hint
Use int number = 0; to create the variable.
2
CONFIGURATION: Start the try block
Add a try block that will contain the division operation 100 / number.
Java
Hint
Write try { to start the block.
3
CORE LOGIC: Complete the try, catch, and finally blocks
Inside the try block, create an integer result that divides 100 by number. Then add a catch block for ArithmeticException that prints "Cannot divide by zero". Finally, add a finally block that prints "End of program".
Java
Hint
Remember to put the division inside try, handle the exception in catch, and always print the final message in finally.
4
OUTPUT: Run the program and print the messages
Run the program so it prints the messages from the catch and finally blocks.
Java
Hint
Make sure your program prints both messages exactly as shown.
Practice
(1/5)
1.
What is the main purpose of the finally block in Java exception handling?
easy
A. To catch exceptions thrown in the try block
B. To throw new exceptions
C. To execute code regardless of whether an exception occurs or not
D. To declare exceptions that a method can throw
Solution
Step 1: Understand the role of try-catch-finally
The try block contains code that might throw exceptions, catch handles them, and finally runs code after both.
Step 2: Identify the purpose of finally
The finally block always executes, whether an exception occurs or not, to finalize or clean up resources.
Final Answer:
To execute code regardless of whether an exception occurs or not -> Option C
Quick Check:
finally always runs = B [OK]
Hint: finally always runs after try/catch blocks [OK]
Common Mistakes:
Thinking finally only runs if an exception occurs
Confusing finally with catch block
Assuming finally can catch exceptions
2.
Which of the following is the correct syntax to add a finally block after a try-catch in Java?