在我的 Java 期末考试中,我们有一个关于 try、catch 和 finally 调用的测试“异常”部分。当我尝试将示例代码放入 Eclipse 中时,我在 catch 和 throw 新区域中遇到错误。所有错误均显示“无法解析为类型”。
如何解决这个问题,以便我可以了解/查看代码应该做什么?
第四季度
public static void main(String [] args)
{
Q4Exception q1 = new Q4Exception();
try{
q1.sampleMethod();
try{
q1.sampleMethod();
}
//This catch does not throw an error
catch(RuntimeException es)
{
System.out.println("A");
}
//This catch below throws the error of cannot be resolved to a type
catch(IOException es)
{
System.out.println("B");
}
//This catch does not throw an error
catch(Exception e)
{
System.out.println("C");
}
finally{
System.out.println("D");
}
}catch(Exception e)
{
System.out.println("E");
}
finally{
System.out.println("F");
}
}
Q4Exception类
public void sampleMethod() throws Exception
{
try{
throw new IOException("H");
}
catch(IOException err)
{
System.out.println("I");
throw new RuntimeException("J");
}
catch(Exception e)
{
System.out.println(e.toString());
System.out.println("K");
throw new Exception(“L");
}
catch(Throwable t)
{
System.out.println("M");
}
finally{
System.out.println("N");
}
}
最佳答案
我认为值得一提的是,在 Eclipse 中,Ctrl+Shift+O 可以为您解决导入问题。
关于java - IOException 无法解析为类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59738046/