Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

java - Why am I getting "must be caught or declared to be thrown" on my program?

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

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) {
                                        ^

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

For example:

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

Resources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...