为什么我的程序会出现“必须捕获或声明为可抛出”?

9

我已经在这个程序上工作了相当长的时间,我的大脑已经炸了。我需要有人帮忙看看。

我正在尝试制作一个程序,逐行读取文本文件,并将每一行制成一个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) {
                                        ^

我做错了什么?

6个回答

15

在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.
}

资源:


阅读教程以获取示例。 - camickr

8

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

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

如果你的方法需要响应某些操作,那么请捕获异常。如果你的调用者需要知道失败的原因,请声明该异常已被抛出。

这两者并不是互斥的。有时候捕获异常并进行一些操作后重新抛出该异常或一个包装了原始异常(即“原因”)的新异常是很有用的。

RuntimeException及其子类无需声明。


0

你倒可以做

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

通常情况下,捕获类本身并将逻辑分为捕获或使用instanceof进行非常特定的日志记录并不是一个坏主意。

0

好的集成开发环境(IDE)要么为您创建catch块,要么将异常添加到方法声明中。

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


0

每当出现“未报告的异常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。 - disrvptor

-3

我曾经遇到过同样的问题。通过添加Spring库“org.springframework.core”,我解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接