Java URL object - new java.net.URL ()

I am 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 the symbol: hp, which would make me believe that the url object, hp, is not created by the line:

 URL hp = new URL("http://www.java2s.com"); 

But shouldn't the catch message report an error?

I tried compilation without try-catch blocks, but I got the error message "Unformed MalformedURLException exception: should be detected or declared that it will be thrown"

If I delete the last line related to hp, the program compiles and runs, but just displays β€œdid it all work?”.

I am sure there is a simple explanation here, but I am not very good at java. Thanks

+4
source share
2 answers

Other answers have given you some helpful tips for preventing errors. But I would like to try to explain how your understanding of what 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 generally referred to by the compiler as a "character") with the name hp , which can point to an instance of a URL; and he creates an instance of the url and makes hp for it.

You interpreted the error as meaning "hp urp object is not created." So, first of all, hp not an object - it is most likely a reference to an object, and, of course, it can be null , and in this case it is a reference to nothing. But the hp symbol exists within its declaration, regardless of whether an object reference is assigned to it.

If the creation of the object failed - i.e. part of the new URL ... statement new URL ... this operator failed - most likely the exception would have occurred as you expected. But even if, for some unclear reason, the creation failed but did not raise an exception, the likely result would be that hp would be null , in which case a valid attempt to dereference the hp variable would result in a NullPointerException .

All this just illustrates that the error you received has nothing to do with whether hp was assigned a value and simply indicates that hp not declared in the area in which you are trying to use it.

The problem is that the try block creates its own scope, so the variables declared inside it are not available outside the block. You will get exactly the same error if the first line inside the try block just reads the URL hp; . As shown in other answers, permission to do this is to declare hp outside the try block, so that a later reference is valid. (It would also work to move the last line into a try block, but it makes sense to limit the contents of this block only to statements that require specific error handling.)

+5
source

When you define hp inside a try-catch block, its visibility is inside the try block. Therefore, you get a compilation error in the print statement outside the try-catch block.

Define hp before the start of the try block as follows: -

 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 section 14.4.2 Scope of Local Variable Declarations here .

Also, for a safer and more correct coding practice, you should throw a MalformedURLException that you catch with throw : -

 catch (MalformedURLException e){ System.err.println("New URL failed"); System.err.println("exception thrown: " + e.getMessage()); throw new MalformedURLException("Invalid URL!"); } 

You also need to update your main object to throw this exception: -

public static void main(String[] args) throws MalformedURLException

If you do not, your code will continue if the URL is incorrect!

+5
source

All Articles