为什么我会“必须被抓住或被宣布被扔"?在我的程序上? [英] Why am I getting "must be caught or declared to be thrown" on my program?

查看:24
本文介绍了为什么我会“必须被抓住或被宣布被扔"?在我的程序上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个程序上工作了很长一段时间,我的大脑被炸了.我可以从旁观者那里得到一些帮助.

I have been working on this program for quite sometime and my brain is fried. I could use some help from someone looking in.

我正在尝试制作一个逐行读取文本文件的程序,每一行都被制成一个 ArrayList 以便我可以访问每个令牌.我究竟做错了什么?

I'm trying to make a program that reads a text file line by line and each line is made into an ArrayList so I can access each token. What am I doing wrong?

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);
    }
}

我要戳出我的眼球了.我遇到了三个错误:

I am about to poke out my eyeballs. I got three errors:

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

我做错了什么?

推荐答案

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

When you work with I/O in Java most of the time you have to handle IOException which can occur any time when you read/write or even close the stream.

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

You have to put your sensitive block in a try//catch block and handle the exception here.

例如:

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

<小时>

资源:

这篇关于为什么我会“必须被抓住或被宣布被扔"?在我的程序上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆