Java 把异常传递给控制台

最简答而又不用写多少代码就能保护异常信息的方法,就是把它们从main()传递到控制台,对于简单的程序可以像这样:

package exceptions;
//: exceptions/MainException.java
import java.io.*;

public class MainException {
  // Pass all exceptions to the console:
  public static void main(String[] args) throws Exception {
    // Open the file:
    FileInputStream file =
      new FileInputStream("MainException.java");
    // Use the file ...
    // Close the file:
    file.close();
  }
} ///:~

 通过把它传递到控制它这里就不必写try-catch子句了.

原文地址:https://www.cnblogs.com/jiangfeilong/p/10303244.html