[英]Throw error. How can I fix this? I've tried many things, but I am new to it and the logic is a little beyond me
The line with "private static BufferedReader" is where the problem lies. 问题所在是带有“ private static BufferedReader”的行。 "-unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" is the error. 错误是“-未报告的异常java.io.FileNotFoundException;必须捕获或声明为抛出”。 Here is my current code: 这是我当前的代码:
import java.io.*;
import java.util.*;
public class PG2ERC
{
private static ArrayList<Integer> arl = new ArrayList<Integer>();
private static BufferedReader br = new BufferedReader(new FileReader("pageRefString.txt"));
public static void main(String[] args) throws IOException, FileNotFoundException
{
String [] arr;
int n1 = 3;
int n2 = 4;
int f;
int pf1 = 0;
int pf2 = 0;
arr = br.readLine().split(" ");
for(String s:arr)
{
f=Integer.parseInt(s);
if(arl.contains(f))
{
arl.remove(arl.indexOf(f));
arl.add(f);
}
else if(arl.size() < n1)
{
++pf1;
arl.add(f);
}
else
{
arl.remove(0);
arl.add(f);
++pf1;
}
f=Integer.parseInt(s);
if(arl.contains(f))
{
arl.remove(arl.indexOf(f));
arl.add(f);
}
else if(arl.size() < n2)
{
++pf2;
arl.add(f);
}
else
{
arl.remove(0);
arl.add(f);
++pf2;
}
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("pg2out.txt"))))
{
writer.write(("Number of page faults is ") + pf1);
writer.write(("Number of page faults is ") + pf2);
}
}
}
}
The problem is more simply demonstrated like this: 该问题更简单地显示为:
import java.io.*;
class Test {
private static Reader reader = new FileReader("foo.txt");
}
The problem is that the static initializer for your class can throw an exception. 问题是您的类的静态初始化器可能引发异常。 That's entirely separate to your main
method. 这与您的main
方法完全分开。
Now in your case, the simplest solution is to change your fields to local variables: 现在,根据您的情况,最简单的解决方案是将字段更改为局部变量:
// No need to declare FileNotFoundException - it's a subclass of IOException anyway
public static void main(String[] args) throws IOException
{
ArrayList<Integer> arl = new ArrayList<Integer>();
BufferedReader br = new BufferedReader(new FileReader("pageRefString.txt"));
... rest of method as before ...
}
At that point, the code that can throw an exception is within a method that declares that those exceptions can be thrown, so it's fine. 那时,可以引发异常的代码在声明可以引发这些异常的方法之内,所以很好。
If you do need to initialize static variables like this, you should do so in a static initializer block: 如果确实需要像这样初始化静态变量,则应在静态初始化程序块中进行初始化:
private static BufferedReader br;
static {
try {
br = new BufferedReader(new FileReader("pageRefString.txt"));
} catch (IOException e) {
// Go bang hard - RuntimeException isn't a checked exception
throw new RuntimeException(e);
}
}
Set your BufferedReader
static variable to null outside the main
method. 在main
方法之外,将BufferedReader
静态变量设置为null。 and initialize in main
method will fix the issue. 并在main
方法中初始化将解决此问题。
private static BufferedReader br = null;
public static void main(String[] args) throws IOException, FileNotFoundException
{
br = new BufferedReader(new FileReader("pageRefString.txt"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.