| Author |
Malformed URL Exception
|
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hello everybody, I have the following program which catches a MalformedURLException. The value of the string variable URL is initialized to null. Instead of this, can I get the URL as an input from the user and then check to see if it is in the URL format. I am not sure how this would be done. Here is the code
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Can you not jast pass the string from the user to the java.net.URL constructor? The constructor throws a MalformedURLException if it cannot parse the string.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Let me try that, Pallavi
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hello Barry, I tried adding the constructor of java.net.URL I am pasting the modified code. However, I get this error: [CODE} C:\JCreator LE\MyProjects\Test1\Ex5.7\ThrowEx.java:22: ';' expected URL u = new URL(url) throw new MalformedURLException("test"); ^ [/CODE] Thank you Pallavi
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Well, look at the line the compiler shows you: URL u = new URL(url) throw new MalformedURLException("test"); Does this line make any sense? What exactly is it supposed to do?
|
"I'm not back." - Bill Harding, Twister
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hi Jim, This line is supposed to create an object of class URL and send a string object into its constructor.If the string specifies an unknown protocol, then an exception will be thrown. I know to catch the Malformed URL exception, it has to reported somewhere. I had tried moving the throw statement to the method urltest and making it a throws statement. I think I am missing a very simple point. I just don't know what. Thanks Pallavi
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
When you describe what you want the code to do, you used the word "if". Look again at the line of code. There's no "if". It looks as though two different statements have been combined without any puctuation.
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hello Jim, The if statement is for an unknown protocol. I tried the follwong code but I am still not clear. The if part , how do we specify the unknown protocol. The only thing I could think of is the URL being a null. I appreciste your patience on this. Thank you Pallavi
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hello Jim, I worked on it a little more and came up with this: However I get these two errors: C:\Pallavi's\Javah\JCreator LE\MyProjects\Test1\Ex5.7\ThrowEx.java:27: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown u = new URL(url); ^ C:\Pallavi's\Javah\JCreator LE\MyProjects\Test1\Ex5.7\ThrowEx.java:30: exception MalformedURLException is never thrown in body of corresponding try statement catch(MalformedURLException e) ^ 2 errors Process completed. Thanks Pallavi
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hello Jim, I tried again and this code compiles without error: What do you think ? Pallavi
|
 |
 |
|
|
subject: Malformed URL Exception
|
|
|