IOException can not be resolved to a type error

2020-03-26 07:55发布

问题:

For my final in Java we have a "exceptions" part on the test with try, catch, and finally calls. When I try to put the example code into Eclipse I get errors in the catch and throw new areas. All of the errors say "Can not be resolved to type".

How do I fix this so I can learn/review what the code is supposed to be doing?

Q4 Class

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 Class

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"); 
 } 
 } 

回答1:

I think it's worth mentioning that in Eclipse, Ctrl+Shif+O does the job of resolving the imports for you.



回答2:

Oh, guess I could answer my own question here. Didn't know I had to import the IOException from java.io!



回答3:

Easy to just use

import java.io.*

for the imports



回答4:

I discovered I was using an old version of JWT , the issue is gone after using the a newer version of JWT dependency .