Table of Contents
In Java, it’s possible to use a try
block without a catch
block, but it must be followed either by a finally
block or be part of a try-with-resources
statement.
Let’s see both cases with the help of examples:
1. try-finally Structure
try
block with a finally
block. As you may know, the finally
block always executes, even if there is an exception or a return
statement in the try
block, except in the case of System.exit()
.
The try-finally
structure allows developers to execute clean-up code regardless of whether an exception occurs in the try
block. Although no catch
block is present to handle exceptions, the finally
block ensures that necessary clean-up actions, such as closing file streams or releasing resources, are performed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
try { // Code that might throw an exception FileInputStream file = new FileInputStream("input.txt"); int data = file.read(); System.out.println("Data read from file: " + data); } finally { // Clean-up code, like closing the file System.out.println("Closing the file"); if (file != null) { file.close(); } } |
Output:
1 2 3 4 |
Data read from file: [content of the first byte] Closing the file |
2. try-with-resources Statement
Introduced in Java 7, the try-with-resources
statement simplifies resource management by automatically closing resources that implement the AutoCloseable
or Closeable
interfaces.
1 2 3 4 5 6 |
try (FileInputStream file = new FileInputStream("input.txt")) { int data = file.read(); System.out.println("Data read from file: " + data); } |
Output:
1 2 3 |
Data read from file: [content of the first byte] |
3. FAQs
Now, let’s go through some frequently asked questions related to using a try
block without a catch
block:
3.1. What Happens When You Have return Statement in try Block?
If you include a return
statement within the try
block, the finally
block will still execute. It’s important to note that the finally
block executes before the print()
function returns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class TryWithoutCatchMain { public static void main(String args[]) { System.out.println(print()); } public static String print() { try { System.out.println("Executing try block"); return "Return from try block"; } finally { System.out.println("Executing finally block"); } } } |
When you execute the above program, you will get the following output:
1 2 3 4 5 |
Executing try block Executing finally block Return from try block |
3.2. What Happens if You Have Return Statement in Finally Block Too?
It overrides whatever the try
block returns. Let’s understand this with an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; public class TryWithoutCatchMain { public static void main(String args[]) { System.out.println(print()); } public static String print() { try { System.out.println("Executing try block"); return "Return from try block"; } finally { System.out.println("Executing finally block"); return "Return from finally block"; } } } |
When you execute the above program, you will get the following output:
1 2 3 4 5 |
Executing try block Executing finally block Return from finally block |
3.3 What if an Exception Is Thrown in the Try Block?
If an exception is thrown in the try
block without catch block, the finally
block still executes. The exception is propagated up the call stack until it finds a matching catch
block. If no catch
block is found, the program may terminate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class TryWithoutCatchMain { public static void main(String args[]) { System.out.println(print()); } public static int print() { try { throw new NullPointerException(); } finally { System.out.println("Executing finally block"); } } } |
When you execute the above program, you will get the following output:
1 2 3 4 5 6 |
Executing finally block Exception in thread "main" java.lang.NullPointerException at org.arpit.java2blog.TryWithoutCatchMain.print(TryWithoutCatchMain.java:14) at org.arpit.java2blog.TryWithoutCatchMain.main(TryWithoutCatchMain.java:7) |
As you can see, even though the code threw a NullPointerException, the finally
block was still executed.
3.4 Why would someone use a try
block without a catch
block?
A try
block might be used without a catch
block when the intention is not to handle the exception at that point in the code but to ensure that the finally
block executes for cleanup purposes, such as closing resources.
You can go through top 50 core java interview questions for more such questions.