当前位置: 首页 > news >正文

try(){}用法try-with-resources、try-catch-finally

在这里插入图片描述

属于Java7的新特性。
经常会用try-catch来捕获有可能抛出异常的代码。如果其中还涉及到资源的使用的话,最后在finally块中显示的释放掉有可能被占用的资源。
但是如果资源类已经实现了AutoCloseable这个接口的话,可以在try()括号中可以写操作资源的语句(IO操作),会在程序块结束时**自动释放掉占用的资源**,不用再在finally块中手动释放了。

例子:
没使用该特性

如果要释放的资源多得话,判断加catch占得篇幅大,不美观也不精简
在这里插入图片描述用了这个特性后代码可以精简些
在这里插入图片描述
try-catch-finally
在这里插入图片描述

try-with-resources
在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述----------------------------------------------------分割线---------------------------------------------------------------------
取自:https://jenkov.com/tutorials/java-exception-handling/try-with-resources.html
Using Multiple Resources
You can use multiple resources inside a Java try-with-resources block and have them all automatically closed. Here is an example of using multiple resources inside a try-with-resources block:
在这里插入图片描述
This example creates two resources inside the parentheses after the try keyword. An FileInputStream and a BufferedInputStream. Both of these resources will be closed automatically when execution leaves the try block.
Closing Order
The resources declared in a Java try-with-resources construct will be closed in reverse order of the order in which they are created / listed inside the parentheses. In the example in the previous section, first the will be closed, then the FileInputStream.
Custom AutoClosable Implementations
The Java try-with-resources construct does not just work with Java’s built-in classes. You can also implement the java.lang.AutoCloseable interface in your own classes, and use them with the try-with-resources construct.

The AutoClosable interface only has a single method called close(). Here is how the interface looks:
在这里插入图片描述
Any class that implements this interface can be used with the Java try-with-resources construct. Here is a simple example implementation:
在这里插入图片描述
The doIt() method is not part of the AutoClosable interface. It is there because we want to be able to do something more than just closing the object.

Here is an example of how the MyAutoClosable is used with the try-with-resources construct:
在这里插入图片描述
Here is the output printed to System.out when the method myAutoClosable() is called:
在这里插入图片描述
As you can see, try-with-resources is a quite powerful way of making sure that resources used inside a try-catch block are closed correctly, no matter if these resources are your own creation, or Java’s built-in components.

举例
在这里插入图片描述

参考:
Java必须懂的try-with-resources

java知识大全

jenkov.com

http://www.lryc.cn/news/66823.html

相关文章:

  • 常见Http错误码学习
  • qemu-基础篇——ARM 链接过程分析(六)
  • Java企业工程项目管理系统+spring cloud 系统管理+java 系统设置+二次开发
  • Eureka与Zookeeper的区别
  • 顺序表和链表的各种代码实现
  • C# 介绍三种不同组件创建PDF文档的方式
  • 极简面试题 --- Redis
  • 可视化图表API格式要求有哪些?Sugar BI详细代码示例(4)
  • 学习vue(可与知乎合并)
  • 【UEFI实战】Linux下如何解析ACPI表
  • Java-Redis持久化之RDB操作
  • 信号signal编程测试
  • Linux学习记录——이십삼 进程信号(2)
  • Revit中如何创建曲面嵌板及一键成板
  • STM32F4_DHT11数字温湿度传感器
  • WiFi(Wireless Fidelity)基础(十一)
  • 操作系统—— 精髓与设计原理--期末复习
  • 每天一道算法练习题--Day21 第一章 --算法专题 --- ----------位运算
  • D1. LuoTianyi and the Floating Islands (Easy Version)(树形dp)
  • rk3588移植ubuntu server
  • 如何更好地刷力扣
  • 上采样和下采样
  • 小猪,信息论与我们的生活
  • 【鸿蒙应用ArkTS开发系列】- http网络库使用讲解和封装
  • 【Java零基础入门篇】第 ⑥ 期 - 异常处理
  • 计算职工工资
  • 2019年上半年软件设计师下午试题
  • IS200TPROH1BCB用于工业应用和电力分配等。高压型隔离开关用于变电站
  • 【MySql】数据库 select 进阶
  • CVPR 2023 | VoxelNeXt实现全稀疏3D检测跟踪,还能结合Seg Anything