*
The moose likes Beginning Java and the fly likes Malformed URL 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 "Malformed URL Exception" Watch "Malformed URL Exception" New topic
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: 18671
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: 18671
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Malformed URL Exception
 
Similar Threads
why my client program will not respond when a string "1" is sent by my server program
please clear my doubt
ExceptionCollection
try catch...........
how read a file text from internet ?