giáo trình Java By Example phần 8
Số trang: 46
Loại file: pdf
Dung lượng: 111.84 KB
Lượt xem: 23
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu giáo trình java by example phần 8, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 8Review Questions 1. How do you use a try program block? 2. How do you use a catch program block? 3. Do you have to catch all types of exceptions that might be thrown by Java? 4. When a method you call is defined as potentially throwing an exception, do you have to handle that exception in your program? 5. How many exceptions can you associate with a single try block? 6. How do you pass an exception up from a called method to the calling method? 7. What are the two main types of exceptions that Java may throw?Review Exercises 1. Write an applet that creates a button object. Set up exception-handling code for the OutOfMemoryException exception that could possibly occur when Java tries to allocate resources for the button. 2. Write an applet that catches all Exception objects and displays the string returned by the Exception objects getMessage() method. (Not all Exception objects return message strings. Test your program by generating a divide-by-zero error, which will cause Java to throw an ArithmeticException exception. This exception does generate a message string.) You can find the solution to this exercise in the CHAP30 folder of this books CD-ROM. The applet is called ExceptionApplet4. Figure 30.8 shows what the applet looks like while running under Appletviewer. Figure 30.8 : ExceptionApplet4 displays the message string returned by an Exception objects getMessage() method. 3. Write an applet that enables the user to enter values into an array. Use two TextField objects, the first being where the user shouldenter the index at which to place the value, and the second being the value to add to the array. Set up the applet so that it responds to ArrayIndexOutOfBoundsException and NumberFormatException exceptions. You can find the solution to this exercise in the CHAP30 folder of this books CD-ROM. The applet is called ExceptionApplet5. Figure 30.9 shows what the applet looks like while running under Appletviewer. Figure 30.9 : This is ExceptionApplet5 running under Appletviewer. http://www.ngohaianh.infoChapter 28Communications CONTENTS URL Objects q Example: Creating an URL Object r URL Exceptions r The Applet Context q Example: Using an AppletContext to Link to an URL r Example: Using an AppletContext in an Applet r Creating a Favorite URLs Applet q Summary q Review Questions q Review Exercises qNot to state the obvious, but because applets are used on the Internet, they have the ability to perform afew types of telecommunications tasks. One of these tasks, connecting to other Web sites, is a snap toimplement. Other tasks, such as accessing data in files, are difficult to implement because you constantlystumble over the security restrictions built into applets. Dealing with the intricacies of Internet security isbeyond the scope of this book. If youre interested in this topic, you should pick up an advanced Javabook. In this chapter, though, youll get a chance to use Java to communicate over the Internet byconnecting to URLs that the user supplies.URL ObjectsIn the previous chapter, you got a quick introduction to URL objects when you obtained the location ofgraphics and sound files by calling the getDocumentBase() and getCodeBase() methods. Youused the URL objects returned by these methods in order to display images and play sounds that werestored on your computer. In that case, the locations of the files were on your own system. What youdidnt know then is that you can create an URL object directly by calling its constructor. Using thistechnique, you can create URL objects that represent other sites on the World Wide Web.Although the URL classs constructor has several forms, the easiest to use requires a string argumentholding the URL from which you want to create the object. Using this constructor, you create the URLobject like this: http://www.ngohaianh.info URL url = new URL(str);This constructors single argument is the complete URL of the location to which you want to connect.This URL string must be properly constructed or the URL constructor will throw an exception (generatean error). Youll soon see what to do about such errors.Example: Creating an URL ObjectSuppose you want to create an URL object for the URL http://www.sun.com, which is where youcan find lots of information about Java. Youd create the URL object like this: URL url = new URL(http://www.sun.com);If the URL construction goes okay, you can then use the URL object however you need to in your applet.URL ExceptionsAs I mentioned previously, if the argument for the URL constructor is in error (meaning that it doesnt usevalid URL syntax), the URL class throws an exception. Because the URL class is designed to throw anexception when necessary, Java gives you no choice except to handle that exception properly. Thisprevents the applet from accidentally attempting to use a defective URL object. Youll learn all the detailsabout handling exceptions in Chapter 30, Exceptions. For now, though, you need to know how tohandle the URL exception because your applets will not compile properly until you add theexception-handling code.Basically, when you need to watch out for an exception, you enclose the code that may generate the errorin a try program block. If the code in the block generates an exception, you handle that exception in acatch program block. (Its no coincidence that when code throws an exception, Java expects t ...
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 8Review Questions 1. How do you use a try program block? 2. How do you use a catch program block? 3. Do you have to catch all types of exceptions that might be thrown by Java? 4. When a method you call is defined as potentially throwing an exception, do you have to handle that exception in your program? 5. How many exceptions can you associate with a single try block? 6. How do you pass an exception up from a called method to the calling method? 7. What are the two main types of exceptions that Java may throw?Review Exercises 1. Write an applet that creates a button object. Set up exception-handling code for the OutOfMemoryException exception that could possibly occur when Java tries to allocate resources for the button. 2. Write an applet that catches all Exception objects and displays the string returned by the Exception objects getMessage() method. (Not all Exception objects return message strings. Test your program by generating a divide-by-zero error, which will cause Java to throw an ArithmeticException exception. This exception does generate a message string.) You can find the solution to this exercise in the CHAP30 folder of this books CD-ROM. The applet is called ExceptionApplet4. Figure 30.8 shows what the applet looks like while running under Appletviewer. Figure 30.8 : ExceptionApplet4 displays the message string returned by an Exception objects getMessage() method. 3. Write an applet that enables the user to enter values into an array. Use two TextField objects, the first being where the user shouldenter the index at which to place the value, and the second being the value to add to the array. Set up the applet so that it responds to ArrayIndexOutOfBoundsException and NumberFormatException exceptions. You can find the solution to this exercise in the CHAP30 folder of this books CD-ROM. The applet is called ExceptionApplet5. Figure 30.9 shows what the applet looks like while running under Appletviewer. Figure 30.9 : This is ExceptionApplet5 running under Appletviewer. http://www.ngohaianh.infoChapter 28Communications CONTENTS URL Objects q Example: Creating an URL Object r URL Exceptions r The Applet Context q Example: Using an AppletContext to Link to an URL r Example: Using an AppletContext in an Applet r Creating a Favorite URLs Applet q Summary q Review Questions q Review Exercises qNot to state the obvious, but because applets are used on the Internet, they have the ability to perform afew types of telecommunications tasks. One of these tasks, connecting to other Web sites, is a snap toimplement. Other tasks, such as accessing data in files, are difficult to implement because you constantlystumble over the security restrictions built into applets. Dealing with the intricacies of Internet security isbeyond the scope of this book. If youre interested in this topic, you should pick up an advanced Javabook. In this chapter, though, youll get a chance to use Java to communicate over the Internet byconnecting to URLs that the user supplies.URL ObjectsIn the previous chapter, you got a quick introduction to URL objects when you obtained the location ofgraphics and sound files by calling the getDocumentBase() and getCodeBase() methods. Youused the URL objects returned by these methods in order to display images and play sounds that werestored on your computer. In that case, the locations of the files were on your own system. What youdidnt know then is that you can create an URL object directly by calling its constructor. Using thistechnique, you can create URL objects that represent other sites on the World Wide Web.Although the URL classs constructor has several forms, the easiest to use requires a string argumentholding the URL from which you want to create the object. Using this constructor, you create the URLobject like this: http://www.ngohaianh.info URL url = new URL(str);This constructors single argument is the complete URL of the location to which you want to connect.This URL string must be properly constructed or the URL constructor will throw an exception (generatean error). Youll soon see what to do about such errors.Example: Creating an URL ObjectSuppose you want to create an URL object for the URL http://www.sun.com, which is where youcan find lots of information about Java. Youd create the URL object like this: URL url = new URL(http://www.sun.com);If the URL construction goes okay, you can then use the URL object however you need to in your applet.URL ExceptionsAs I mentioned previously, if the argument for the URL constructor is in error (meaning that it doesnt usevalid URL syntax), the URL class throws an exception. Because the URL class is designed to throw anexception when necessary, Java gives you no choice except to handle that exception properly. Thisprevents the applet from accidentally attempting to use a defective URL object. Youll learn all the detailsabout handling exceptions in Chapter 30, Exceptions. For now, though, you need to know how tohandle the URL exception because your applets will not compile properly until you add theexception-handling code.Basically, when you need to watch out for an exception, you enclose the code that may generate the errorin a try program block. If the code in the block generates an exception, you handle that exception in acatch program block. (Its no coincidence that when code throws an exception, Java expects t ...
Tìm kiếm theo từ khóa liên quan:
lập trình Java tin học ứng dụng lập trình windows lập trình C# mẹo hay cho tin học thủ thuật windowsGợi ý tài liệu liên quan:
-
Tài liệu bồi dưỡng giáo viên sử dụng SGK Tin học 10 Cánh diều (Định hướng Tin học ứng dụng)
61 trang 212 0 0 -
101 trang 193 1 0
-
20 trang 178 0 0
-
Cách gỡ bỏ hoàn toàn các add on trên Firefox
7 trang 166 0 0 -
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 161 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 156 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 152 0 0 -
Giáo trình Tin học ứng dụng: Phần 1 - Trường ĐH Tài nguyên và Môi trường Hà Nội
125 trang 145 0 0 -
Bài giảng Tin học ứng dụng: Kiểm định trung bình - Trường ĐH Y dược Huế
25 trang 137 0 0 -
Giáo trình Quản trị mạng (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
173 trang 118 1 0