Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Lý do cho điều trị đặc biệt là để tạo ra một đối tượng mới, đặc biệt là nhỏ, đơn giản thay đổi isn't rất hiệu quả, bởi vì nơi các đối tượng trên heap. Đối với các loại Java rơi trở lại vào phương pháp của C và C + +. Đó là, thay vì | allows components to reliably communicate problems to client code. Feedback The goals for exception handling in Java are to simplify the creation of large reliable programs using less code than currently possible and with more confidence that your application doesn t have an unhandled error. Exceptions are not terribly difficult to learn and are one of those features that provide immediate and significant benefits to your project. Feedback Exercises Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide available for a small fee from www.BruceEckel.com. 1. Create a class with a main that throws an object of class Exception inside a try block. Give the constructor for Exception a String argument. Catch the exception inside a catch clause and print the String argument. Add a finally clause and print a message to prove you were there. Feedback 2. Create your own exception class using the extends keyword. Write a constructor for this class that takes a String argument and stores it inside the object with a String reference. Write a method that prints out the stored String. Create a try-catch clause to exercise your new exception. Feedback 3. Write a class with a method that throws an exception of the type created in Exercise 2. Try compiling it without an exception specification to see what the compiler says. Add the appropriate exception specification. Try out your class and its exception inside a try-catch clause. Feedback 4. Define an object reference and initialize it to null. Try to call a method through this reference. Now wrap the code in a try-catch clause to catch the exception. Feedback 5. Create a class with two methods f and g . In g throw an exception of a new type that you define. In f call g catch its exception and in the catch clause throw a different exception of a second type that you define . Test your code in main . Feedback 446 Thinking in Java www.BruceEckel.com 6. Repeat the previous