Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Question unreported exception java.io.IOException

    So I'm trying to compile a program that calls the following function:
    Java Code:
    public void parseUrl(String source) throws IOException {
    		
    		...
    		
    		try {
    			....
    			}
    		} 
    		catch (MalformedURLException mue) {
    			
    			System.out.println("MalformedURLException occured");
    			mue.printStackTrace();
    			System.exit(1);
    			
    		} catch (IOException ioe) {
    			
    			System.out.println("IOException occured");
    			ioe.printStackTrace();
    			System.exit(1);
    			
    		}
    There's more to this function, but this is where I'm getting the error, I believe.

    This is the compile error:

    unreported exception java.io.IOException; must be caught or declared to be thrown


    Any ideas? I thought I formatted the try/catch correctly, but I guess not. I'm new to java, so any help would be appreciated.

    Thanks!
    Last edited by fluffaykitties; 03-04-2011 at 03:32 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    The parseUrl method can throw an IOException. Where you call this method you need to handle the exception.

  3. #3
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Don't I handle it here though with the catch part?

  4. #4
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Or should I move the catch clauses to where I actually call the function, and throw the exception on that method?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by fluffaykitties View Post
    Don't I handle it here though with the catch part?
    Yes you do but the method signature says it will throw an IOException. All the compiler cares about is that where you call that method it MUST handle whatever exception is thrown (regardless if one gets thrown or not).

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by fluffaykitties View Post
    Or should I move the catch clauses to where I actually call the function, and throw the exception on that method?
    Totally upto you.

  7. #7
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Oh okay awesome. So all I did was add throws IOException to the method where I call the parseURL exception, and now it works. Thanks!

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Well that is just hiding the problem. Now the method that calls the method that calls parseURL has to handle it. Does it makes sense to do that? Once again the design is upto you.

  9. #9
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Yes, I think that's okay. Because the method that calls it is actually a constructor with just one line in it's body...the call to parseURL. So if parseURL throws the exception, the constructor just won't be created, and the program will exit.

    Correct? (I may be wrong, and if so, please correct me!)

  10. #10
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    anyone? :confused::confused::confused:

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    What happened when you tried? Did it compile? Did it run?

  12. #12
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I have other compile errors that I'm still working on, so I can't test it quite yet.

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  2. unreported IOException problem
    By doha786 in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 11:14 PM
  3. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 02:32 PM
  4. Replies: 2
    Last Post: 09-18-2009, 11:22 PM
  5. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •