Java 如何捕获reset()方法的IOException?

Java 如何捕获reset()方法的IOException?,java,exception,Java,Exception,我正在编写这段代码,不断收到一个运行时错误,告诉我需要捕获IOException。我该怎么做?这是我的代码: System.out.println( "\nThese presidents were born before the states were formed:\n"); // DO NOT REMOVE OR MODIFY ArrayList<String> america = new ArrayList<String>(); String l

我正在编写这段代码,不断收到一个运行时错误,告诉我需要捕获IOException。我该怎么做?这是我的代码:

System.out.println( "\nThese presidents were born before the states were formed:\n");  // DO NOT REMOVE OR MODIFY
    ArrayList<String> america = new ArrayList<String>();
    String l;
    String line;
    while((line = infile2.readLine()) != null) 
    {

        america.add(line);
    }
    Collections.sort(america);
    infile1.reset();
    try
    {
        while((infile1.ready()))
        {
            ArrayList<String> am = new ArrayList<String>();
            l= infile1.readLine();
            String [] ls = l.split(" ");
            for(int i=1; i<ls.length;i++)
            {
                am.add(ls[i]);
            }
            Collections.sort(am);
            america.removeAll(am);
        }
    }
    catch( IOException ex)
    {
        System.out.println("ERROR");
    }

    System.out.println(america);
System.out.println(“\n这些总统出生于各州成立之前:\n”);//不要删除或修改
ArrayList america=新ArrayList();
字符串l;
弦线;
而((line=infile2.readLine())!=null)
{
美国。添加(行);
}
收藏.分类(美国);
infile1.reset();
尝试
{
而((infile1.ready()))
{
ArrayList am=新的ArrayList();
l=infile1.readLine();
字符串[]ls=l.split(“”);

对于(inti=1;i我怀疑您得到的是编译错误,而不是运行时错误

while((line=infile2.readLine())!=null)
包含对方法的调用,该方法可以抛出
IOException
,但您尚未使用
try catch
对其进行包装

在这里调用
infie1.reset()
可能存在同样的问题


这是因为在Java中,会检查异常;如果调用的方法声明
抛出footexception
,则需要使用
try{…}catch(footexception e){…}
包装对该方法的调用,或者使用
抛出footexception
标记调用方法(或异常层次结构中的任何异常)。

一般来说,向我们展示您所说的堆栈跟踪/错误输出是一个好主意。