Workshop: Throwing and Catching Exceptions

For this hour's workshop, you will create a class that uses exceptions to tell another class about an error that has taken place. The classes in this workshop are HomePage, a class that represents a personal home page on the World Wide Web, and PageCatalog, an app that catalogs these pages. Enter the text of Listing 18.4 in your text editor, saving it as when you're done.

Listing 18.4. The Full Text of
 1: import*;
 3: public class HomePage {
 4: String owner;
 5: URL address;
 6: String category = "none";
 8: public HomePage(String inOwner, String inAddress)
 9: throws MalformedURLException {
11: owner = inOwner;
12: address = new URL(inAddress);
13: }
15: public HomePage(String inOwner, String inAddress, String inCategory)
16: throws MalformedURLException {
18: this(inOwner, inAddress);
19: category = inCategory;
20: }
21: }

Save the file and compile it, producing a HomePage class you can use in other programs. This class represents personal web pages on the Web. It has three variables: address, a URL object 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 URL objects, HomePage must either deal with MalformedURLException errors in a TRy-catch block 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 tHRows in the two constructor methods, HomePage removes the need to deal with MalformedURLException errors in any way. To create an app that uses the HomePage class, return to your text editor and enter Listing 18.5. Save the file as

Listing 18.5. The Full Text of
 1: import*;
 2: 3: public class PageCatalog {
 4: public static void main(String[] arguments) {
 5: HomePage[] catalog = new HomePage[5];
 6: try {
 7: catalog[0] = new HomePage("Mark Evanier",
 8: "", "comic tutorials");
 9: catalog[1] = new HomePage("Todd Smith",
10: "", "music");
11: catalog[2] = new HomePage("Rogers Cadenhead",
12: "", "programming");
13: catalog[3] = new HomePage("Juan Cole",
14: "", "politics");
15: catalog[4] = new HomePage("Rafe Colburn",
16: "");
17: for (int i = 0; i < catalog.length; i++) {
18: System.out.println(catalog[i].owner + ": " +
19: catalog[i].address + " -- " +
20: catalog[i].category);
21: }
22: } catch (MalformedURLException e) {
23: System.out.println("Error: " + e.getMessage());
24: }
25: }
26: }

When you run the compiled app, the following output will be displayed:

Error: no protocol:

The PageCatalog app creates an array of HomePage objects and then displays the contents of the array. Each HomePage object is created using up to three arguments:

  • The name of the page's owner
  • The address of the page (as a String, not a URL)
  • The category of the page

The third argument is optional, and it is not used in Lines 15–16. The constructor methods of the HomePage class throw MalformedURLException errors when they receive a string that cannot be converted into a valid URL object. These exceptions are handled in the PageCatalog app by using a try-catch block. To correct the problem causing the "no protocol" error, edit Line 16 so that the string begins with the text "http://" like the other web addresses in lines 7–14.

Mark Evanier: -- comic tutorials Todd Smith: -- music Rogers Cadenhead: -- programming Juan Cole: -- politics Rafe Colburn: -- none