Java Reference
In-Depth Information
Error conditions
On occasion, the server encounters an error but returns useful information in the mes‐
sage body nonetheless. For example, when a client requests a nonexistent page from the
www.ibiblio.org
website, rather than simply returning a 404 error code, the server sends
the search page shown in
Figure 7-2
to help the user figure out where the missing page
might have gone.
Figure 7-2. IBiblio's 404 page
The
getErrorStream()
method returns an
InputStream
containing this page or
null
if no error was encountered or no data returned:
public
InputStream
getErrorStream
()
Generally, you'll invoke
getErrorStream()
inside a
catch
block after
getInput
Stream()
has failed.
Example 7-17
demonstrates with a program that reads form the
input stream if possible. However, if that fails for any reason, it then reads from the error
stream instead.
Example 7-17. Download a web page with a URLConnection
import
java.io.*
;
import
java.net.*
;
public
class
SourceViewer4
{