In Java 1.7v following two concepts are introduced to enhance exception handling in Java.
- try-with resources
- Multi-catch block
try with Resources
Let’s consider the following Java code (Java 1.6v).
import java.io.*;
public class TryWithResources {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("input.txt"));
// use br for requirements
} catch (IOException e) {
// handle exception
}
finally {
if(br!=null){
br.close();
}
}
}}
In this approach there are many problems as given below.
- We must have close resources inside the final block, It increases the complexity of programming.
- We have to write the final block compulsory, Hence it increases the length of the code and reduces readability.
To overcome these problems try with resources introduced in Java 1.7v. The main advantage of trying with resources is whatever resource is open as part of the try block will be closed automatically, Hence control is reached at the end of the try either normally or abnormally hence we are not required to close resources explicitly.
public class TryWithResources {
public static void main(String[] args) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader("Input.txt"))){
// Perform desirted operation with buffer reader
}
}}
In this approach(above Java code java 1.7v ) BufferReader object will close automatically once the control reaches the end of the try block either normally or abnormally and we are not responsible to close the BufferReader object explicitly.
But until Java 1.6v it is highly recommended to close resources by writing the finally block with try-catch.
So that after the introduction of trying with resources- the complexity of programming and length of the code is reduced and readability is improved.
Conclusion (try with resources)
- We can declare multiple resources in the try block but all these resources should be separated with semicolon (;).
import java.io.*;
public class TryWithResources {
public static void main(String[] args) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader("Input.txt")); FileReader FR = new FileReader("output.txt")) {
// Perform desired operation
}
}}
- All resources declared inside the try block should be auto-closable. A resource is said to be autoclosable if and only if the corresponding class implements
Java. lang.AutoClosable
interface. All input output-related resources, DataBase-related resources network-related resources are already implemented AutoClosable interface. AutoClosable interface contains only one method-close(
). - All resource reference variables are final, Hence we can not perform reassignment otherwise we will get compile time error-“java: auto-closeable resource br may not be assigned”.
import java.io.*;
public class TryWithResources {
public static void main(String[] args) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader("Input.txt"))){
br = new BufferedReader(new FileReader("Input1.txt"));
}
}}
- Until Java 1.6v try block should be associated with either catch or finally but from Java 1.7v onward we can use try block with resources without catch or finally.
import java.io.*;
public class TryWithResources {
public static void main(String[] args) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader("Input.txt"))){
// Perform Task
}
}}
Similar Java Tutorials
- Odd Even in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java