java - IOException 无法解析为类型错误

标签 java exception try-catch

在我的 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/

相关文章:

Java反射,从方法中提取泛型类型

java - 错误和异常有什么区别?

Java - 简单的异常处理查询

java - 什么是构造try catch语句以仅允许整数的正确方法?

java - JBoss任务调度: pass argument to method

java - 为什么要在返回之前将 volatile 分配给局部变量

C++ 异常处理指南

Java 在使用 Try/Catch 时陷入无限循环

java - try 和finally 中的返回语句

java - 如何暂停/恢复使用 mediarecorder 创建的录音?