Java 为什么我的程序“必须被捕获或被宣布被抛出”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3747155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting "must be caught or declared to be thrown" on my program?
提问by Luron
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?
我正在尝试制作一个逐行读取文本文件的程序,每一行都被制作成一个,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);
}
}
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?
我究竟做错了什么?
采纳答案by Colin Hebert
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.
当您在 Java 中使用 I/O 时,大部分时间您必须处理 IOException,这可能会在您读/写甚至关闭流时发生。
You have to put your sensitive block in a try//catch block and handle the exception here.
您必须将敏感块放在 try//catch 块中并在此处处理异常。
For example:
例如:
try{
// All your I/O operations
}
catch(IOException ioe){
//Handle exception here, most of the time you will just log it.
}
Resources:
资源:
回答by Andy Thomas
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:
Java 在编译时检查异常规范。您必须捕获异常或在方法签名中声明它抛出。以下是您如何声明它可能会从您的方法中抛出:
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.
RuntimeException 及其子类不需要声明。
回答by Tony Ennis
Good IDEs will either create the catch block for you or add the exception to the method declaration.
好的 IDE 会为您创建 catch 块或将异常添加到方法声明中。
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.
请注意,如果您按照 Colin 的解决方案将异常添加到方法声明中,则调用您的方法的任何方法也必须具有合适的 catch 块或在方法声明中声明异常。
回答by user1999777
I have had the same problem. I got it solved by adding the spring library "org.springframework.core"
我曾经也有过一样的问题。我通过添加 spring 库“org.springframework.core”解决了这个问题
回答by Pravin
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.
一般来说,在一段时间内捕获类本身并不是一个坏主意,如果您需要非常具体的日志,则决定将逻辑转换为捕获或执行 instanceof。
回答by Mohit Singh
whenever "unreported exception IOException; must be caught or declared to be thrown" this error has come then required to put the code in try catch block. Example
每当“未报告的异常 IOException;必须被捕获或声明为抛出”时,就会出现此错误,然后需要将代码放入 try catch 块中。例子
try{
// All your I/O operations
}
catch(IOException ioe){
//Handle exception here, most of the time you will just log it.
}