【问题标题】:Why I got "Unhandled exception type IOException"为什么我得到“未处理的异常类型 IOException”
【发布时间】:2016-07-04 16:52:26
【问题描述】:

我正在通过书中的示例学习 Java。 我写了下面的代码并得到“未处理的异常类型 IOException” 为什么?我该如何解决这个问题。我应该声明 IOException 类吗?

import java.nio.file.*;

public class JavaIO {
    public static void main(String[] args) {

        String dirString = "C:/Users/USER/Desktop/Test/Files";
        Path dirPath = Paths.get(dirString);
        if(Files.notExists(dirPath)){
            Files.createDirectory(dirPath);
        }       
        System.out.println("Err");
        System.exit(1);
    }
}

【问题讨论】:

  • 这很可能是因为您没有创建通向“文件”的目录,请改用“createDirectories”。

标签: java exception-handling io


【解决方案1】:

当您在 Java 中处理文件或执行 IO 操作时,有可能由于各种原因您将无法访问文件或资源,这将在 FIleNotFound 或 IO 操作的行上抛出异常条件无法执行。此异常需要处理,因为它属于 Java 中的“已检查”异常。简单地说,您需要自己处理这种情况,或者让子类使用 Throws、throw 或 try/catch 块来处理。

因此,Java 要求您执行“未处理的异常类型 IOException”消息。

【讨论】:

  • 那么我应该在 JAVA 中处理所有可能的异常吗?在其他编程语言中,我不需要这样做。是JAVA的特点吗?
  • 不是全部,只是你被要求的那个,通过使用 Exception 类可以捕获所有方法,这是所有“检查”异常的父类。
【解决方案2】:

查找 Try Catch 并将处理文件的代码部分放入其中。如果文件无法创建或位置不存在,这将处理并报告异常。

【讨论】:

  • 谢谢你!!!!!!我添加了 try catch 语句,但错误仍然发生。我使用 java.io.IOException 而不是 IOException。谢谢!!!!
  • 哦,这只是意味着如果你想在程序中写得更短,你必须导入 java.io.IOException。
【解决方案3】:

因为Files.createDirectory() 可能会抛出java.io.IOException 而你既没有抓住它也没有声明要抛出它。

捕捉异常处理错误

import java.nio.file.*;

public class JavaIO {
    public static void main(String[] args) {

        String dirString = "C:/Users/USER/Desktop/Test/Files";
        Path dirPath = Paths.get(dirString);
        if(Files.notExists(dirPath)){
            try{
                Files.createDirectory(dirPath);
            } catch(java.io.IOException e){
                System.out.println("createDirectory failed:" + e);
            }
        }
        System.out.println("Err");
        System.exit(1);
    }
}

或添加声明以将其抛出以忽略其被抛出的可能性。

import java.nio.file.*;

public class JavaIO {
    public static void main(String[] args) throws java.io.IOException {

        String dirString = "C:/Users/USER/Desktop/Test/Files";
        Path dirPath = Paths.get(dirString);
        if(Files.notExists(dirPath)){
            Files.createDirectory(dirPath);
        }
        System.out.println("Err");
        System.exit(1);
    }
}

【讨论】:

  • 谢谢!!!!!!我添加了 try catch 语句,但错误仍然发生。我使用 java.io.IOException 而不是 IOException。谢谢!!!!
  • @pty115 添加import java.io.IOException;,您不需要完全限定类名。
【解决方案4】:

Files.createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException 这是一个检查 Exception; 要么 catch 它或修改main 以表明它可能被抛出。类似的,

public static void main(String[] args) throws IOException {

或使用try-catch 和catch IOException 之类的

if(Files.notExists(dirPath)){
    try {
        Files.createDirectory(dirPath);
    } catch (IOException e) {
        e.printStackTrace();
    }
 }   

【讨论】:

  • 谢谢!!!!!!我添加了 try catch 语句,但错误仍然发生。我使用 java.io.IOException 而不是 IOException。谢谢!!!!
猜你喜欢
  • 2011-01-19
  • 2021-11-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2015-12-20
相关资源
最近更新 更多