Results 1 to 4 of 4
  1. #1
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default opening an URL -

    I have started a new project

    (I'm using NetBeans)

    I intentionally added a line at a time to make sure I am on the right track

    I am getting this error:

    "unreported exception java.net.MalformedURLException; must be caught or declared to be thrown"

    With this code:
    Java Code:
    import java.net.URLConnection;
    import java.net.URL;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    
    import java.io.*;
    import java.net.*;
    
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
    //        URL yahoo = new URL("http://www.yahoo.com/");
    
    
    //        URL url = new URL;
            URL url = new URL("http://www.yahoo.com");
            URL u;
    
        }
    
    }
    I have added several - I asume unneeded so far "Imports" trying to make it happy.

    The error is with the URL url = new URL("...");

    My code / IDE is not happy with having ("http://yahoo.com") it looks like.

    The line
    Java Code:
    URL u;
    is fine (or at least is not underlined in red)

    Thoughts?

    Thanks
    Sparky

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You should do as the error message suggests -- either catch the exception or throw it. If this doesn't make sense, please have a look at this link that will help: Exceptions

  3. #3
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    I'll follow your link and review catching exceptions.

    Just a comment - some of this seems frustrating. I'm going through some tutorials and seem to make some steps forward and then hit points like this.

    Also this code that generated this error was from a tutorial. The tutorial gave the error.

    Very frustrating.

    What I thought I was doing was defining the "url" as a URL with a web address ready to go.

    Oh well - continue on the study

    -Sparky

  4. #4
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    OK -

    Question from a beginner

    I've spent some time reading through the link you provided on catching exceptions.

    I found a list of exceptions and thanks to the details in teh error I see which exception to catch.

    I need some advice -

    When I insterted my Try / Catch I then had an error regarding it didn't recognize the MalformedURLException class.

    Finding what needs to be imported seems overwhelming - When I scan the JDK library - there are so many files (is classes the correct way to call them?) How do I know what is needed to import and how can I find them quicker?

    Is there an intuitive way to speed of the search - Meaning it took me a long time to find where the Malformed URLExcpetion lived.

    On 3+ occasions I paste in code from a tutorial and something doesn't work - on 2 or so it has been related to the Import statements.

    I know with enough time and experience it may get less frustrating.

    How do you as a seasoned JAVA programmer find what needs to be imported smoothly / quickly?


    Here's my code - granted it doesn't do anything (yet)

    Java Code:
    import java.net.URL;
    
    import java.net.*;
    
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            try{
                    URL url = new URL("http://www.yahoo.com");
            }
            catch (MalformedURLException e)
            {
    //            return null;
            }
            }
            
            URL u;
    
    
        }
    Thanks
    Sparky

Similar Threads

  1. Opening a file
    By rdjava in forum Advanced Java
    Replies: 0
    Last Post: 01-30-2011, 09:34 PM
  2. Replies: 4
    Last Post: 10-07-2009, 10:14 AM
  3. opening ports
    By prashant in forum Networking
    Replies: 2
    Last Post: 03-22-2009, 09:23 PM
  4. Opening in Java and .Net
    By techinvo in forum Jobs Offered
    Replies: 0
    Last Post: 03-22-2009, 08:24 AM
  5. opening of an exe in JFrame
    By smartsubroto in forum New To Java
    Replies: 3
    Last Post: 07-16-2008, 06:01 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
  •