Language Enhancements and Features
Java 7: The Top 8 Features - Part 2
Mandatory catch block when using the try statement
Note that if you are creating AutoClosable resources in the try block, you are required to declare a catch block to catch the concrete exception thrown from the actual AutoClosable. close() method. Alternatively you need to throw the Exception. Think of the close() method as implicitly being called as the last line in the try block. So, if an application has its own AutoClosable class as follows:
Then, the following will cause a compile error:
To fix the above, you need to catch or throw the Exception from the calling method.
Syntax for declaring multiple resources
The try statement can contain multiple statements separated by semicolon. Each statement must result in an AutoClosable object.
It is illegal to declare any variable which isn’t an AutoClosable.
Output
ERROR: try-with-resources not
applicable to variable type
required: AutoCloseable
found: long
AutoClosable vs Closable
The old Closable interface introduced in Java 5, which also has the same method that now extends from AutoClosable, implying that all Closable classes are automatically Auto-Closable. Those classes automatically become resources that can be created in the try statement. The slight difference in AutoClosable and Closable is that unlike Closable.close(), AutoClosable.close() is not supposed to be idempotent, which means that calling it twice can have side effects. The second difference is that since exceptions thrown from AutoClosable. close() are suppressed, AutoClosable.close() should not throw exceptions which can cause problems when suppressed, like the InterruptedException.
#3 More precise rethrow
There are often situations when we want to catch multiple exceptions of different types, do “something” with them, and rethrow them. Let us consider this example, where we have some code which throws IOException and SQLException.
In the above example, we are logging each type of exception being thrown by the try block before rethrowing them. This results in a duplication of code in the catch blocks. Before Java 7, to get around the code duplication issue we would catch the base exception as follows:
However, this requires us to rethrow the base Exception type java.lang.Exception from the calling method.
public static void main(String[] args) throws Exception {
With Java 7
Java 7 has added the “precise rethrow” feature which lets you catch and throw the base exception while still throwing the precise exception from the calling method.
Note the keyword final in the above catch clause. When a parameter is declared final, the compiler statically knows to only throw those checked exceptions that were thrown by the try block and were not caught in any preceding catch blocks.
#4 Multi-catch
There is no elegant way with Java 6 to catch multiple exceptions of different types and handle them in the same way.
You can always catch the parent Exception in order to avoid duplication of code, but it is not always suitable especially if the parent is java.lang.Exception. Java 7 lets you catch multiple Exceptions in a single catch block by defining a “union” of Exceptions to be caught in the catch clause.
Note that the pipe ‘|’ character is used as the delimiter. The variable ‘ex’ in the above example is statically typed to the base class of Ex1 and Ex2, which is java.lang.Exception in this case.
#5 Binary integral literals
With Java 7, you can now create numerical literals using binary notation using the prefix “0b”
int n = 0b100000;
System.out.println("n = " + n);
Output
n = 32
#6 Underscores in numeric literals
With Java 7, you can include underscores in numeric literals to make them more readable. The underscore is only present in the representation of the literal in Java code, and will not show up when you print the value.
Without underscore
int tenMillion = 10000000;
System.out.println(“Amount is “ + tenMillion);
Output
10000000
With underscore
int tenMillionButMoreReadable =
10_000_000;
System.out.println("Amount is " + tenMillionButMoreReadable);
Output
10000000
More rules and examples
1. Consecutive underscores is legal.
int n = 1000______000;
2. Underscores can be included in other numeric types as well.
double d = 1000_000.0d;
long l = 1000_000l;
int hex = 0xdead_c0de;
int bytes = 0x1000_0000;
3. Underscore can be included after the decimal point.
double example8 = 1000_000.000_000d;
4. It is illegal to start a literal with an underscore
5. It is illegal to end a literal with an underscore.
6. It is also illegal to have underscore be just before or after a decimal point.
Pages
- Strings in Switch
- Mandatory catch block when using the try statement
- Improved type inference for generic instance creation

Follow us