Try-with-resources

Try-with-Resources
Make sure you take a closer look at the syntax for try-with-resources block.

try(Scanner consoleScanner = new Scanner(System.in)) {

In this statement, you have acquired the resources inside the parenthesis after the try keyword, but before the try block. Also, in the example, you don’t provide the finally block. The Java compiler will internally translate this try-with-resources block into a try-finally block (of course, the compiler will retain the catch blocks you provide). 
You can acquire multiple resources in the try-with-resources block. Such resource acquisition statements must be separated using semicolons.
Can you provide try-with-resources statements without any explicit catch or finally blocks? Yes! 
Remember that a try block can be associated with a catch block, finally block, or both. 
A try-with-resources statement block gets expanded internally into a try-finally block. 
So, you can provide a try-with-resources statement without explicit catch or finally blocks. 
Listing 7-14 uses a try-with-resources statement without any explicit catch or finally blocks

Listing 7-14. TryWithResources2.java
import java.util.Scanner;
class TryWithResources2 {
    public static void main(String [] args) {
        System.out.println("Type an integer in the console: ");
        try(Scanner consoleScanner = new Scanner(System.in)) {
            System.out.println("You typed the integer value: " + consoleScanner.nextInt());
        }
    }
}


Although it is possible to create a try-with-resources statement without any explicit catch or finally, it doesn’t mean you should do so! 
For example, since this code does not have a catch block, if you type some invalid input, the program will crash.

D:\> java TryWithResources2
Type an integer in the console:
ten
Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Scanner.java:864)
 at java.util.Scanner.next(Scanner.java:1485)
 at java.util.Scanner.nextInt(Scanner.java:2117)
 at java.util.Scanner.nextInt(Scanner.java:2076)
 at TryWithResources2.main(TryWithResources2.java:7)


So, the benefit of a try-with-resources statement is that it simplifies your life by not having to provide finally blocks explicitly. 
However, you still need to provide necessary catch blocks.

Listing 7-14. TryWithResources2.java
import java.util.Scanner;
class TryWithResources2 {
	public static void main(String [] args) {
		System.out.println("Type an integer in the console: ");
		try(Scanner consoleScanner = new Scanner(System.in)) {
			System.out.println("You typed the integer value: " + consoleScanner.nextInt());
		}catch(Exception io){
            //还是需要的
        }finally{
            //自动的去关闭了
        }
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值