ptg7068951
Throwing and Catching Exceptions 259
The classes in this project are HomePage, a class that represents a personal
home page on the Web, and PageCatalog, an application that catalogs
these pages.
Enter the text of Listing 18.4 in a new empty Java file called HomePage.
LISTING 18.4 The Full Text of HomePage.java
1: importjava.net.*;
2:
3: public classHomePage {
4: String owner;
5: URL address;
6: String category= “none”;
7:
8: publicHomePage(String inOwner, String inAddress)
9: throwsMalformedURLException {
10:
11: owner= inOwner;
12: address= new URL(inAddress);
13: }
14:
15: publicHomePage(String inOwner, String inAddress, String inCategory)
16: throwsMalformedURLException {
17:
18: this(inOwner, inAddress);
19: category= inCategory;
20: }
21: }
You can use the compiled HomePageclass in other programs. This class
represents personal web pages on the Web. It has three instance variables:
address, a URLobject representing the address of the page; owner, the person
who owns the page; and category, a short comment describing the page’s
primary subject matter.
Like any class that creates URLobjects, HomePagemust either deal with
MalformedURLExceptionerrors in a try-catchblock or declare that it is
ignoring these errors.
The class takes the latter course, as shown in Lines 8–9 and Lines 15–16. By
using throwsin the two constructor methods, HomePageremoves the need
to deal with MalformedURLExceptionerrors in any way.
To create an application that uses the HomePageclass, return to NetBeans
and create an empty Java file called PageCatalogthat contains the text of
Listing 18.5.