【问题标题】:Why am I getting "must be caught or declared to be thrown" on my program?为什么我的程序会出现“必须被抓到或被宣布被扔”?
【发布时间】:2010-09-19 19:32:46
【问题描述】:

我已经在这个程序上工作了很长一段时间,我的大脑被炸了。我可以向正在查看的人寻求帮助。

我正在尝试制作一个程序,它可以逐行读取文本文件,并且每一行都被制成ArrayList,这样我就可以访问每个令牌。我究竟做错了什么?

import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.rmi.server.UID;
import java.util.concurrent.atomic.AtomicInteger;

public class PCB {
    public void read (String [] args) {
        BufferedReader inputStream = null;

        try {
            inputStream = new BufferedReader(new FileReader("processes1.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                write(l);
            }
        }
        finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    public void write(String table) {
        char status;
        String name;
        int priority;

        ArrayList<String> tokens = new ArrayList<String>();

        Scanner tokenize = new Scanner(table);
        while (tokenize.hasNext()) {
            tokens.add(tokenize.next());
        }

        status = 'n';
        name = tokens.get(0);
        String priString = tokens.get(1);
        priority = Integer.parseInt(priString);

        AtomicInteger count = new AtomicInteger(0);
        count.incrementAndGet();
        int pid = count.get();

        System.out.println("PID: " + pid);
    }
}

我的眼珠子都快戳出来了。我遇到了三个错误:

U:\Senior Year\CS451- Operating Systems\Project1 PCB\PCB.java:24: unreported exception java.io.IOException; must be caught or declared to be thrown
            inputStream.close();}
                             ^
U:\Senior Year\CS451- Operating Systems\Project1 PCB\PCB.java:15: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
        inputStream = new BufferedReader(new FileReader("processes1.txt"));
                                         ^
U:\Senior Year\CS451- Operating Systems\Project1 PCB\PCB.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
        while ((l = inputStream.readLine()) != null) {
                                        ^

我做错了什么?

【问题讨论】:

    标签: java exception exception-handling


    【解决方案1】:

    当您在 Java 中使用 I/O 时,大部分时间您必须处理 IOException,这可能在您读/写甚至关闭流时随时发生。

    您必须将敏感块放入 try//catch 块中并在此处处理异常。

    例如:

    try{
        // All your I/O operations
    }
    catch(IOException ioe){
        //Handle exception here, most of the time you will just log it.
    }
    

    资源:

    【讨论】:

    • 阅读示例教程。
    【解决方案2】:

    Java 在编译时检查异常规范。您必须捕获异常或在方法签名中声明它抛出。以下是您如何声明它可能会从您的方法中抛出:

       public void read (String [] args) throws java.io.IOException {
    

    如果您的方法需要做一些响应,则捕获异常。如果您的调用者需要知道失败,请将其声明为已抛出。

    这些不是相互排斥的。有时捕获异常、执行某些操作并重新抛出异常或包装原始异常(“原因”)的新异常很有用。

    RuntimeException 及其子类不需要声明。

    【讨论】:

      【解决方案3】:

      好的 IDE 会为您创建 catch 块或将异常添加到方法声明中。

      请注意,如果您按照 Colin 的解决方案将异常添加到方法声明中,则任何调用您的方法的方法也必须具有合适的 catch 块或在方法声明中声明异常。

      【讨论】:

        【解决方案4】:

        你宁愿这样做

        try{
            // All your I/O operations
        }
        catch(Exception e){
            //Handle exception here, most of the time you will just log it.
        }
        

        一般来说,在某些时候捕获类本身并决定你的逻辑是 catch 还是在需要非常具体的日志时执行一个 instanceof 并不是一个坏主意。

        【讨论】:

          【解决方案5】:

          每当“未报告的异常 IOException;必须被捕获或声明为抛出”时 然后需要将代码放入 try catch 块中。 示例

          try{
              // All your I/O operations
          }
          catch(IOException ioe){
              //Handle exception here, most of the time you will just log it.
          }
          

          【讨论】:

          • 或者封闭方法可以将异常声明为可抛出本身。此外,您可能希望使用 try-with-resources 语法,而不仅仅是 try/catch。
          【解决方案6】:

          我也遇到了同样的问题。我通过添加spring库“org.springframework.core”解决了它

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多