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