Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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 application that catalogs these pages.

Enter the text of Listing 18.4 in your text editor, saving it as HomePage.java when you're done.

Example 18.4. The Full Text of HomePage.java

 1: import java.net.*; 
 2: 
 3: public class HomePage { 
 4:     String owner; 
 5:     URL address; 
 6:     String category = "none"; 
 7: 
 8:     public HomePage(String inOwner, String inAddress) 
 9:         throws MalformedURLException { 
10: 
11:         owner = inOwner; 
12:         address = new URL(inAddress); 
13:     } 
14: 
15:     public HomePage(String inOwner, String inAddress, String inCategory) 
16:         throws MalformedURLException { 
17: 
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 application that uses the HomePage class, return to your text editor and enter Listing 18.5. Save the file as PageCatalog.java.

Example 18.5. The Full Text of PageCatalog.java

 1: import java.net.*; 
 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("Carl Steadman", 
 8:                 "http://www.freedonia.com/~carl", "angst"); 
 9:             catalog[1] = new HomePage("Greg Knauss", 
10:                 "http://www.eod.com", "humor"); 
11:             catalog[2] = new HomePage("Rogers Cadenhead", 
12:                 "http://workbench.cadenhead.info", "programming"); 
13:             catalog[3] = new HomePage("Joshua Marshall", 
14:                 "http://www.talkingpointsmemo.com", "politics"); 
15:             catalog[4] = new HomePage("Matt Haughey", 
16:                 "a.wholelottanothing.org"); 
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:         } catch (MalformedURLException e) { 
22:             System.out.println("Error: " + e.getMessage()); 
23:         } 
24:     } 
25: } 

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

Error: no protocol: a.wholelottanothing.org 

icon05.gif

The PageCatalog application 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 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 application 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.

Carl Steadman: http://www.freedonia.com/~carl — angst 
Greg Knauss: http://www.eod.com — humor 
Rogers Cadenhead: http://workbench.cadenhead.info — programming 
Joshua Marshall: http://www.talkingpointsmemo.com — politics 
Matt Haughey: http://a.wholelottanothing.org — none 

icon05.gif

Share ThisShare This

Informit Network