Java 什么';如果出现错误,终止程序的最佳方法是什么?

Java 什么';如果出现错误,终止程序的最佳方法是什么?,java,Java,在捕捉到异常并在主函数中处理它之后,您认为关闭程序的最佳方式是什么 返回 系统退出(…) 重新抛出异常 将自定义异常作为包装抛出 抛出运行时异常 别的 public static List<String> readFile(String file) throws NoSuchFileException, EOFException, IOException { Path p = Paths.get(file); if (!Files.exists(p))

在捕捉到异常并在主函数中处理它之后,您认为关闭程序的最佳方式是什么

  • 返回
  • 系统退出(…)
  • 重新抛出异常
  • 将自定义异常作为包装抛出
  • 抛出运行时异常
  • 别的

    public static List<String> readFile(String file)
            throws NoSuchFileException, EOFException, IOException {
        Path p = Paths.get(file);
        if (!Files.exists(p)) {
            throw new NoSuchFileException(file);
        } else if (!Files.isRegularFile(p)) {
            throw new NoRegularFileException(file);
        } else if (!Files.isReadable(p)) {
            throw new AccessDeniedException(file);
        } else if (Files.size(p) == 0) {
            throw new EOFException(file);
        }
        return Files.readAllLines(p);
    }
    
    public static void main(String[] args) {
        try {
            if (args.length != 2) {
                System.out.println("The proper use is: java MyProgram file1.txt file2.txt");
                return;
            }
    
            List<List<String>> files = new ArrayList<>();
            for (String s : args) {
                try {
                    files.add(Utilities.readFile(s));
                } catch (NoSuchFileException e) {
                    System.out.printf("File %s does not exist%n", e.getMessage());
                    System.exit(-1);
                } catch (NoRegularFileException e) {
                    System.out.printf("File %s is not a regular file%n", e.getMessage());
                    throw e;
                } catch (AccessDeniedException e) {
                    System.out.printf(
                        "Access rights are insufficient to read file %s%n", e.getMessage()
                    );
                    throw new ReadFileException(e);
                } catch (EOFException e) {
                    System.out.printf("File %s is empty%n", e.getMessage());
                    throw new RuntimeException(e);
                }
            }
            //some other code
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    公共静态列表读取文件(字符串文件)
    抛出NoSuchFileException、EOFEException、IOException{
    路径p=Path.get(文件);
    如果(!Files.exists(p)){
    抛出新的NoSuchFileException(文件);
    }如果(!Files.isRegularFile(p)){
    抛出新的NoRegularFileException(文件);
    }如果(!Files.isReadable(p))则为else{
    抛出新的AccessDeniedException(文件);
    }else if(Files.size(p)==0){
    抛出新的EOFEException(文件);
    }
    返回文件。readAllLines(p);
    }
    公共静态void main(字符串[]args){
    试一试{
    如果(参数长度!=2){
    System.out.println(“正确的用法是:javamyprogramfile1.txt file2.txt”);
    回来
    }
    列表文件=新的ArrayList();
    for(字符串s:args){
    试一试{
    添加(Utilities.readFile);
    }捕获(NoSuchFileException){
    System.out.printf(“文件%s不存在%n”,e.getMessage());
    系统退出(-1);
    }捕获(NOregularfilee异常){
    System.out.printf(“文件%s不是常规文件%n”,e.getMessage());
    投掷e;
    }捕获(AccessDeniedException e){
    System.out.printf(
    “访问权限不足,无法读取文件%s%n”,e.getMessage()
    );
    抛出新的ReadFileException(e);
    }捕获(EOFEException e){
    System.out.printf(“文件%s为空%n”,e.getMessage());
    抛出新的运行时异常(e);
    }
    }
    //其他代码
    }捕获(例外e){
    e、 printStackTrace();
    }
    

  • 编辑:我应该明确指出,在main结束之前,程序中会有一些其他代码,所以我不能让它结束。

    如果您希望它是干净的(即不向用户显示stacktrace),那么抛出异常是不可能的(至少在main之外)。然后,关键是您是否希望从程序返回退出代码(如果不希望)


    如果要返回退出代码,必须使用
    System.exit(int errcode);
    。否则可以直接返回(或让
    main
    正常退出)。

    这取决于如何定义“最佳”。如果catch是main方法的最后一部分,则catch将执行,main仍将结束。如果要执行错误报告,则始终可以登录catch块。我建议您最好记录异常,而不是只是抛出异常或打印错误消息。此外,打印错误消息时应使用
    错误流
    System.err.println(“您的消息”);
    )。除此之外,使用
    System.exit(1)退出程序
    应该可以完成这项工作。@Kayaman在这里这样做看起来并不奇怪。结果总是一样的,但我想知道我的一些选项是否使用不当。从任何方法返回;只从
    main
    方法退出。记录任何异常。在
    catch
    块被激活和禁用时处理代码不只是打印一条错误消息。然后返回它。