Why am I getting “must be caught or declared to be thrown” on my program?

白昼怎懂夜的黑 提交于 2019-12-28 02:07:31

问题


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

What am I doing wrong?


回答1:


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:

  • oracle.com - Lesson: Exceptions



回答2:


Java checks exception specifications at compile time. You must either catch the exception or declare it thrown in your method signature. Here's how you would declare that it may be thrown from your method:

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

Catch the exception if your method needs to do something in response. Declare it as thrown if your caller needs to know about the failure.

These are not mutually exclusive. Sometimes it is useful to catch the exception, do something and re-throw the exception or a new exception that wraps the original (the "cause").

RuntimeException and its subclasses do not need to be declared.




回答3:


Good IDEs will either create the catch block for you or add the exception to the method declaration.

Be advised that if you add the exceptions to the method declaration as per Colin's solution, any method that invokes your method will also have to have a suitable catch block or declare the exception in the method declaration.




回答4:


You could rather do

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

in general its not a bad idea to catch the class itself some time and decides you logic into catch or do a instanceof if you need very specific log.




回答5:


I have had the same problem. I got it solved by adding the spring library "org.springframework.core"



来源:https://stackoverflow.com/questions/3747155/why-am-i-getting-must-be-caught-or-declared-to-be-thrown-on-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!