• Post Reply Bookmark Topic Watch Topic
  • New Topic

unreported Exception error  RSS feed

 
Shweta Grewal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a peice of code:

it gives following error:
unreported exception java.io.IOException; must be caught or declared to be thrown
[javac] if (!aSocket.isClosed()) aSocket.close();
Can someone tell me what is this error and how to solve it?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably close method throws IOException which is checked, so you/developer need to handle that exception either by catching or *throws*/propagate from your method.

*just surround the method/close using try/catch idiom.
 
Shweta Grewal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*throws*/propagate from your method

How to do that?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shweta Grewal wrote:
*throws*/propagate from your method

How to do that?


The error message is telling you exactly what's wrong. You either need to catch the IOException, or you need to declare throws IOException in your method declaration. Google for java exception tutorial for more details.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should look in the API. For example this is the socket class documentation; if you look at the close() method, it tells you whether it throws any Exceptions. And I agree with JV’s suggestion you look for the Java™ Tutorials, which are well worth reading: try here.
You should bookmark the API and the tutorial in your web browser.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that exception is thrown from a finally, you should consider putting the finally inside a try block. Here is an example for BufferedReaders; I would think you can close a Socket similarly.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!