• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unreported Exception error

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

*throws*/propagate from your method


How to do that?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic