Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Exceptions
- Assertions
- Workshop: Throwing and Catching Exceptions
- Summary
- Q&A
- Quiz
- Questions
- Activities
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
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
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 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 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
Summary | Next Section

Account Sign In
View your cart