0
0
Javaprogramming~30 mins

Finally block in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Make sure your program prints both messages exactly as shown.