版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/88607149
In simple programs, the easiest way to preserve exceptions without writing a lot of code is to pass them out of main() to the console.
// exceptions/MainException.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.nio.file.*;
import java.util.*;
public class MainException {
// Pass exceptions to the console:
public static void main(String[] args) throws Exception {
// Open the file:
List<String> lines = Files.readAllLines(Paths.get("MainException.java"));
// Use the file ...
}
}
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/MainException.java