【发布时间】:2013-12-12 18:35:04
【问题描述】:
对于我的 Java 期末考试,我们在测试中有一个“异常”部分,包括 try、catch 和 finally 调用。当我尝试将示例代码放入 Eclipse 时,我在 catch 中遇到错误并抛出新的区域。所有错误都显示“无法解析输入”。
我该如何解决这个问题,以便我可以了解/查看代码应该做什么?
第四节课
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");
}
}
Q4异常类
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");
}
}
【问题讨论】:
-
你导入
IOException了吗? -
import java.io.IOException