对于Java的最终学习,我们在测试中有一个“异常”部分,其中包括try,catch和finally调用。当我尝试将示例代码放入Eclipse时,遇到了错误并抛出了新的领域。所有错误都显示“无法解析键入”。

如何解决此问题,以便可以学习/查看代码应该执行的操作?

Q4类

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可以为您解决导入问题。

09-30 17:00
查看更多