の紹介+のメリット
- try-とリソース参照リンク(を見てお勧めしますが、内容は非常に有益ですが、私は少し補足をしました)https://www.cnblogs.com/itZhy/p/7636615.html
- IOUtilsソースhttps://blog.csdn.net/zmx729618/article/details/51888938/
小さな栗のために
ポンポンシンプルな依存性
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
ラン1
public static void main(String[] args) {
try (FileInputStream inputStream = new FileInputStream(new File("test.txt"))) {
// 读取到控制台
System.out.println(inputStream.read());
// 关闭输入流 , 该方法需要上面的pom依赖
IOUtils.closeQuietly( inputStream );
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
結果1
Exception in thread "main" java.lang.RuntimeException: test.txt (系统找不到指定的文件。)
at Main.main(Main.java:16)
Caused by: java.io.FileNotFoundException: test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at Main.main(Main.java:10)
:見つけることができるIOUtils.closeQuietly( inputStream );
ストリームエラー情報が投げ出されていないから近いし、nullポインタcloseQuietly法の裁きを抑え珍しいことではないので!!これがあります。
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException var2) {
;
}
}
同時に、異常の抑制があります
抗コンパイルされたコードを見た後、
public class Main {
public Main() {
}
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream(new File("test.txt"));
Throwable var2 = null;
try {
System.out.println(inputStream.read());
IOUtils.closeQuietly(inputStream);
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (inputStream != null) {
if (var2 != null) {
try {
inputStream.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
inputStream.close();
}
}
}
} catch (IOException var14) {
throw new RuntimeException(var14.getMessage(), var14);
}
}
}
コードを逆コンパイル、あなたは例外を処理するための特殊なコードがあることに気づくことがあります。
var2.addSuppressed(var11);
- これは、別の知識のtry-とリソース文法を含む、異常な抑制と呼ばれています。外部リソースが処理されている場合(例えば読み取りまたは書き込み)、あなたは例外が発生した場合、および外部リソースのクローズその後の工程で、異常苦しんできた、あなたは、外部リソースの処理中に発生したことをキャッチします閉じたリソースエラーに遭遇し、異常は「抑制」が、異常をgetSuppressed方法によって廃棄されていない、抽出することができる異常が抑制されます。
- トライで-リソース、外部リソースへの外部リソースのクローズ処理とは、異常な苦戦している場合は、「異常閉」が抑制され、「処理の例外が」スローされますが、「例外OFF」失われていません、しかし、例外リストに保存されているの「例外処理」を抑えます。
異常解除阻害
加点pom
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<!-- log -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
用途は()e.getSuppressedというコードを追加し、例外をスローしませんでした鬱積を取得
@Slf4j
public class Main {
public static void main(String[] args) {
try (FileInputStream inputStream = new FileInputStream(new File("test.txt"))) {
// 读取到控制台
System.out.println(inputStream.read());
// 关闭输入流
IOUtils.closeQuietly( inputStream );
} catch (IOException e) {
Throwable[] suppressed = e.getSuppressed();
for (Throwable throwable : suppressed) {
log.error("", throwable);
}
throw new RuntimeException(e.getMessage(), e);
}
}
}
サンプルのセットを作成します。
对外部资源的处理和对外部资源的关闭均遭遇了异常, 太难了, 造不出来.....................
如果你可以的, 麻烦在下面贴一下`英雄帖`!
問題の注意コンパイルされたバージョン、少なくともJDK5は、ポンポンにJDKコンパイル済みのバージョンを指定してください
ポンポンを指定することができます
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>