Quote:
Originally Posted by
pict3000
...why I turned to the forum for help...
Hmmm... Let me summarize another user's similar experience:
1. Googled "java programming forum help" First non-advertising hit was this forum. This must be the best, right?
2. Signed up for this forum. Read forum rules. Posted specific question about some code. Put code in code tags. Was told to use Google.
Annoying, isn't it?
Anyhow...
Try to compile the example but leave off the part about main(...) throws Exception:
In other words change line 6 from
to the following:
Code java:
public static void main(String[] args)
Now compile and what do you get?
Well...
Here's what the compiler tells me:
Code :
URLReader.java:11: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
url = new URL("http://www.xxxzaphodsxxx.com/");
^
URLReader.java:13: unreported exception java.io.IOException; must be caught or declared to be thrown
BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream()));
^
URLReader.java:16: unreported exception java.io.IOException; must be caught or declared to be thrown
while ((inputLine = in.readLine()) != null)
^
URLReader.java:18: unreported exception java.io.IOException; must be caught or declared to be thrown
in.close();
See: It is telling me that some of the methods used in main() can throw exceptions. The compiler insists on telling me this. Furthermore, it won't even compile the program unless I "do something" about it. I either have to catch the exceptions that it flagged, or I can declare something about the exceptions.
If you don't want to be bothered putting try{}catch(){} blocks around the flagged method calls in your program, you can reassure the compiler that you recognize the situation by putting the generic "throws Exception" thing in the main() declaration.
Then, any exception thrown by an underlying method can be "caught" here (in main) and passed along. Some people (who don't know about decisions that went into defining the language) have suggest that it might have been less confusing if the terminology said something about main "catches" exceptions rather than main "throws" exceptions. Makes no difference what we think. Gotta follow the rules.
Anyhow...
Now, if an exception occurs from any underlying method and we don't catch it, the throwee will print a generic message and abort the program. Maybe that's good enough.
On the other hand, if you want to supply additional information before bailing out of the program or if you want to handle the exception in a way that gives the program a chance to recover, that's what the try{}catch(){} stuff is all about.
Note that ou can also give a comma-separated list of the exceptions that you will not catch and write try{}catch(){} blocks for the others. Etc., etc., etc.
Note that, even with that generic "throws Exception" thing you can still write specific try{}catch(){} blocks for specific method calls in your program. Continue on with the tutorial and if you have further questions, try posting again.
Oh, yeah. About that example code that you posted...
Now here's something interesting (interesting to me, that is) that I found when running it.
Note that, apparently because of the popularity of that tutorial, someone has actually snagged that URL shown in the example code, and you may not get an exception if you run it as shown. (At least I didn't. A server at yourURLcom actually responded. Brilliant!)
So...
If you actually want to see an exception, try a url something like http://xxxzaphodsxxx.info[ in the program
Anyhow...
If you are still interested, you can play around with something like
Code java:
import java.net.*;
import java.io.*;
public class Z
{
public static void main(String[] args)
{
URL url = null; // Must be declared outside of the block so that the compiler won't complain
try {
// Checks syntax of URL expression; doesn't check to see if it
// can be reached
// This valid url syntax, but no server can be reached here
url = new URL("http://xxxzaphodsxxx.info/");
// For testing: Give something that it can't handle
// Malformed URL: No protocol given
//url = new URL("//xxxzaphodsxxx.info/");
}
catch(MalformedURLException e) {
System.out.println("Bad URL syntax");
e.printStackTrace();
System.out.println("Tttthat's all, Folks!");
System.exit(1);
} // End catch MalformedURLException
System.out.println("After instantiating url");
// A single block that surrounds all IO
// Not necessarily the best thing to do in general, but it's easy to make a point
try {
BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (IOException e) {
System.out.println("Bummer: BAD IO");
e.printStackTrace();
System.exit(1);
} // End catch IOException
} // end main()
} // end class URLReader
My output:
Code :
After instantiating url
Bummer: BAD IO
java.net.UnknownHostException: xxxzaphodsxxx.info
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:175)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at sun.net.NetworkClient.doConnect(NetworkClient.java:178)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.http.HttpClient.New(HttpClient.java:338)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:935)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:876)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:801)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
at java.net.URL.openStream(URL.java:1029)
at Z.main(Z.java:31)
If I comment out the good url line and uncomment the bad url line
Code java:
//url = new URL("http://xxxzaphodsxxx.info/");
// For testing: Give something that it can't handle
// Malformed URL: No protocol given
url = new URL("//xxxzaphodsxxx.info/");
Here's what I see:
Code :
Bad URL syntax
java.net.MalformedURLException: no protocol: //xxxzaphodsxxx.info/
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at Z.main(Z.java:18)
Tttthat's all, Folks!
Now go back to the original code (with main(...) throws Exception) and no try{}catch(){} blocks and use the url examples that I showed above.
Cheers!
Z