In this tutorial we are going to see new features added to try-catch in Java 7.
Resources in try statement
One of the most important feature added in java 7 is auto closing of resources like InputStream, BufferedInputStream,etc.In previous versions of Java, any resources opened in try statement had to be manually closed in finally block, but in Java 7, the try with resource statement ensure that any resources declared in the try statement will be closed automatically.If multiple resource variables are declared in the try statement are closed in reverse order that they are declared
Old try Statement
String path = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
System.out.println(br.readLine());
}catch (IOException e) {
e.printStackTrace();
}
finally {
if (br != null)
br.close();
}
In above code I want to point out three things
1.See the program I declared the BufferedReader outside the try block just so that it can be accessed in finally block.
2. There is need to initialize BufferedReader to null, so that it is guaranteed to be initialized when we access it in finally block
3.The br.close() in finally bock throw an exception and it hiding original exception thrown in try block exception from caller. so that we need to handle the exception thrown from is.close() and throw the original IOException.
Handle excetion in Finall block
String path = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
System.out.println(br.readLine());
}catch (IOException e) {
e.printStackTrace();
}
finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In above code still have problem.The exception thrown from finally is suppressed and not accessible to the calling code.If we want both exception from try block and finally block , we have to do the following thing.
Suppressed Exception
String path = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path));
System.out.println(br.readLine());
}catch (IOException e) {
e.printStackTrace();
}
finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
SuppressedException.getThreadlocal().setException(e);
}
}
in above code SuppressedException is used defined java bean with the field named suppressed of type Exception.The calling code can call suppressedException.getThreadlocal().getException() to get the exception suppressed in finally clause. But java 7 all the problems are handled by try with resource statement
Java 7 Try statement
String path = "";
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
System.out.println(br.readLine());
}
Each resource in try statement should create an object which implements the new java.lang.AutoClosable interface.The AutoClosable iterface consist of only one method.
void close() throws Exception{
}
Each resource created in try statement closed automatically.If an exception thrown in try block and another exception thrown while closing resource .
The first exception is the one eventually thrown to the caller. The second exception is available to caller via the ex.getSupressed() method.
0 comments:
Post a Comment