This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Malformed URL Exception

 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try that,
Pallavi
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jim,
I tried again and this code compiles without error:

What do you think ?
Pallavi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic