I'm a very new to java, just trying to run some simple programs. I have this code:
import java.net.*;
import java.io.*;
class example1 {
public static void main(String args[]){
try{
URL hp = new URL("http://www.java2s.com");
System.out.println("it all worked?");
}catch (MalformedURLException e){
System.err.println("New URL failed");
System.err.println("exception thrown: " + e.getMessage());
}
System.out.println(hp.getProtocol());
}
}
The java compiler "cannot find symbol: hp" which would lead me to believe that the url object, hp is not being created by the line:
URL hp = new URL("http://www.java2s.com");
But shouldn't the catch statement be reporting an error?
I tried compiling without the try-catch blocks but I was getting an error saying "unreported exception MalformedURLException; must be caught or declared to be thrown"
If i remove the last line that refers to hp, the program compiles and runs but just displays "it all worked?".
I'm sure there is a simple explanation here but I don't have much knowledge of java. Thanks
The other answers have given you some useful advice on avoiding the error. But I would like to try to explain how your understanding of what the error means is confused.
This line:
URL hp = new URL("http://www.java2s.com");
does two things at once. It declares a variable (which is more generally referred to by the compiler as a "symbol") named hp
, which can point to an instance of URL; and it creates an instance of URL and makes hp
point to it.
You interpreted the error to mean "the url object hp is not being created". So, first of all, hp
is not an object -- it is at most a reference to an object, and of course it can also be null
, in which case it is a reference to nothing. But the symbol hp
exists, within the scope of its declaration, regardless of whether an object reference is assigned to it.
If the object creation had failed -- i.e. the new URL ...
portion of the statement had failed -- then most likely an exception would have occurred as you expected. But even if for some obscure reason the creation had failed but not thrown an exception, the likely result would be that hp
would be null
, in which case a valid attempt to dereference the variable hp
would result in a NullPointerException
.
All of which is just to illustrate that the error you received has nothing to do with whether hp
has been assigned a value, and simply indicates that hp
has not been declared within the scope in which you are attempting to use it.
The issue is that a try
block creates its own scope, so variables declared within it are not accessible outside the block. You would receive exactly the same error if the first line inside your try
block read simply URL hp;
. As shown in the other answers, the resolution to this is to declare hp
outside the try
block, so that the later reference is valid. (It would also work to move the last line into the try
block, but it makes sense to limit the contents of that block only to the statements that require the specific error handling.)
When you define hp
inside the try-catch block, its visibility is within the try block. Hence, you get the compilation error in the print statement outside the try-catch block.
Define hp
before the try block begins in the following manner:-
URL hp = null;
try{
hp = new URL("http://www.java2s.com");
System.out.println("it all worked?");
System.out.println(hp.getProtocol());
}catch (MalformedURLException e){
System.err.println("New URL failed");
System.err.println("exception thrown: " + e.getMessage());
}
To understand this better read the section 14.4.2 Scope of Local Variable Declarations
here.
Also for safer and proper coding practice, you should throw the MalformedURLException
that you catch with the throw clause:-
catch (MalformedURLException e){
System.err.println("New URL failed");
System.err.println("exception thrown: " + e.getMessage());
throw new MalformedURLException("Invalid URL!");
}
You will also need to update your main to throw this exception:-
public static void main(String[] args) throws MalformedURLException
If you do not do this your code will continue in case of a malformed URL!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With