This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception" Watch "exception" New topic
Author

exception

Philip Mat
Greenhorn

Joined: Jun 10, 2010
Posts: 10
I'm new to Java and I have the below code which when compiled gives the error:- unreported exception java.io.IOexception; must be caught or declared to be thrown.
Since I'm catching the IOException in my code, I think I don't really need to declare the IOException to be thrown on main method. It compiles successfully only when I declare the IOException to be thrown on main method.

What can be done to make this code compile successfully without declaring the IOException to be thrown on main method?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names aren't acceptable. You can change your display name here. Thanks!

Now, as to your question: Those "close" calls can throw IOException, and they're not in a try block. Yes, this is a pain, and yes, everybody hates this. Solution? Another try block inside the finally block, which catches the IOException.


[Jess in Action][AskingGoodQuestions]
Philip Mat
Greenhorn

Joined: Jun 10, 2010
Posts: 10
Thank you ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
What Ernest means is something like that. Only I may have got some {} unbalanced because I am not used to reading indentation with more than one brace per line.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: exception
 
Similar Threads
Procuing Output to a text file is showing peculier behaviour
If conditional not branching correctly
I/O streams
Please explain me.
Unknown Source